This question already has answers here:
Copying nested lists in Python
(3 answers)
What is the difference between shallow copy, deepcopy and normal assignment operation?
(12 answers)
Closed 2 years ago.
a = [[1], [2, 3], [4, 5, 6]]
b = a[:]
a[0] = "x"
a[1][0] = 10
print(a)
print(b)
Why the result is (--why 10 changed the original object?)
['x', [10, 3], [4, 5, 6]]
[[1], [10, 3], [4, 5, 6]]
Related
This question already has answers here:
How to deep copy a list?
(10 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
What is the best way to copy a list? [duplicate]
(7 answers)
Closed 1 year ago.
Let's assume that a is some reference variable and I want to put it into a list three times:
a = [1,2,3]
b = [a]*3
>>> b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
So this still works as expected. But if I append an element to one of the reference variables it is reflected to all of them:
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
Does this happen because the * operator in the first code block only copies the references and the list only points to a? Do I need a for-loop in order to be able to have a list of independent reference variables (copies) n times? (where n is 3 in the example above)
How excactly does the * operator work in this instance?
With a for-loop and the append()-Method the behaviour is the same - are references also used here?:
>>> a = [1,2,3]
>>> b = []
>>> for i in range(2):
... b.append(a)
...
>>> b
[[1, 2, 3], [1, 2, 3]]
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3, 4]]
When inserting copies the behaviour is as expected:
>>> a = [1,2,3]
>>> b = []
>>> for i in range(2):
... b.append(a.copy())
...
>>> b
[[1, 2, 3], [1, 2, 3]]
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3]]
Is there a shorter, more convenient way to do this than to loop with .append() and .copy() as I did above?
My Python-Version is Python 3.8.6
Note that my questions go beyond just using deep_copy() as it is about inserting elements more often in a list and the *-operator - I know what deep_copy() is!
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 years ago.
is there any way to keep list a unmodified by changing list b?
a = [1,2,3]
b = a
print(a)
print(b)
b[0]=100
print(a)
print(b)
Output:
[1, 2, 3]
[1, 2, 3]
[100, 2, 3]
[100, 2, 3]
Yes, make a copy of list a:
a = [1, 2, 3]
b = a.copy()
...
By copying it, you get a new list with identical contents.
This question already has answers here:
How to add an integer to each element in a list?
(12 answers)
Closed 5 years ago.
A = [[1,2,3],[4,5,6]]
def addOne(a):
return a+1
addOne(A) doesn't work, obviously because I don't want A+1,
I want to get [[[2,3,4]],[5,6,7]]
[addone(x) for x in y for y in A] doesn't work either, I think list comprehensions doesn't work that way.
note, addOne is just a placeholder for a more complicated function
>>> nums = [[1, 2, 3], [4, 5, 6]]
>>> def add_one(a):
... return a + 1
...
>>> [list(map(add_one, sublist)) for sublist in nums]
[[2, 3, 4], [5, 6, 7]]
>>> [[add_one(num) for num in sublist] for sublist in nums]
[[2, 3, 4], [5, 6, 7]]
This question already has answers here:
Rolling or sliding window iterator?
(29 answers)
Closed 6 years ago.
I would like to reorder a python list:
a = [1,2,3,4,5,6,7,...]
to the following form:
[[1,2,3],[2,3,4],[3,4,5],...]
What is the fastest way to do that?
you can try:
>>> a = [1,2,3,4,5,6,7]
>>> new_list = []
>>> for index in range(len(a)-2):
new_list.append(a[index:index+3])
>>> new_list
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Suppose I have a list of lists, like [[1, 2], [3, 4], [5, 6], [7, 8]]. What is the most elegant way in python to get [1, 2, 3, 4, 5, 6, 7, 8]?
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
Or:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
res=[]
for item in mylistOfList:
res+=item