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.
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:
Convert tuple to list and back
(11 answers)
Closed 2 years ago.
a = [1, 2, 3]
b = [4, 5, 6]
res = next(zip(a, b))
print(res)
Output:
(1,4)
how to get every pair in aa a separate list?
Expected Output:
[1,4]
[2,5]
[3,6]
you can zip the items in a list and unpack them in a list comp.
new_list = [[x,y] for x,y in zip(a,b)]
print(new_list)
[[1, 4], [2, 5], [3, 6]]
the issue with your solution is your first turning your lists into an iterator (correct) but you are only calling the first value with next
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]]
This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 years ago.
As I don't know what title should be given to my this confusion so I'm putting it just a doubt
a = [1,2,3,4,5]
b = a
for i in range(len(a)):
c = (i - 4)
print(a)
print(b)
b[c] = a[i]
print(a)
print(b)
output
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 1, 4, 5]
[1, 1, 1, 4, 5]
...
why values of list a is getting in each step of loop?
Your problem lies in this line:
b = a
This doesn't do what you think it does. In particular, it does not make a copy of a. After the assignment, both b and a refer to the same object. Thus, any change to b is reflected in a also.
One way to force a shall copy is to use the slice syntax:
b = a[:]
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 7 years ago.
As a hypothetical example:
>>> a = [1, 2, 3]
>>> b = a
>>> b[0] = 4
>>> b
[4, 2, 3]
>>> a
[4, 2, 3]
I know that this occurs because the references to both a and b are identical, and therefore point to the same bytes of memory. A change in the bytes referenced by b will be reflected in a, since they share the same memory.
What is the best way to work around this, so that a remains the original value, and after b is made from a, changes to b will not go to a, also.
Specific code:
outputs = []
a = [[0,2,5],[4,2,0],[6,0,0]]
for i in range(3):
for j in range(3):
if not a[i][j]:
b = a
b[i][j] = 1
outputs.append(b)
This returns:
outputs = [[[1,2,5],[4,2,1],[6,1,1]],
[[1,2,5],[4,2,1],[6,1,1]],
[[1,2,5],[4,2,1],[6,1,1]]]
You can copy the original list by slicing it:
a = [1, 2, 3]
b = a[:]
Or using the built in list list() funtion:
b = list(a)
You could also use list comprehension in place of your nested for loops:
b = [[i if i else 1 for i in j] for j in a]
This :
new_list = list(old_list)
And so this will happen -
>>> a = [1,2,3]
>>> b = list(a)
>>> b.append(8)
>>> b
[1, 2, 3, 8]
>>> a
[1, 2, 3]