Appending a value in variable changes both values [duplicate] - python

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 5 years ago.
Look at the code:
a = ['Hello']
b = a # ['Hello']
b.append(2)
print b # ['Hello', 2]
print a # ['Hello', 2]
Here, a is assigned to b, meaning that the change of value in a can affect the value of b. How can the change in b can affect a in this case?
Is it that List in python have any special rule where appending a value can affect both a and b?

Because it's exactly the same value. Names are bound to references, which means that b = a points both to the same list.

Related

why does variable assignment not work on lists sometimes [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 2 years ago.
Python beginner here. Confused about the nature of list assignment.
a = [1, 2, 3]
b = a.reverse()
>>>a
>>>[3,2,1]
>>>b
>>> # nothing shows up
or
e = [1,2,3]
f = e.append(4)
>>>e
>>>[1,2,3,4]
>>>f
>>> # nothing shows up
Why does assigning to b or f not work here.
I believe it has to do with the mutability of lists? Am i completely wrong? Thanks
Both these methods do an in-place modification, i.e.:
b = a.reverse()
f = e.append(4)
They modify the original object and do not create a new one. Hence, you see None when you try and print these.
As per #juanpa.arrivillaga:
Note: The fact that these methods return None is a convention.

Add an element to a list, the output is none [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 4 years ago.
I'm very confused about a situation. Why I cannot assign a list to a variable. Can someone explain it to me?
a =[1,2,3,4]
b = a.insert(0,1)
print(b)
the output is
None
The insert() method only inserts the element to the list. It doesn't return any value.
Adding to the replies that you got, if it is the list b that needs to be updated but not the list a, you should proceed as such:
a =[1,2,3]
b = list(a)
b.insert(1,20)
print(a, b)

How to copy a list over in python without keeping them linked? [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 5 years ago.
I tried:
A = [1,2,3]
B = A
B[0] = A[0]*3
should give:
B == [3,2,3]
True
A == [1,2,3]
True
but what it really does:
B == [3,2,3]
A == [3,2,3]
how do I copy A over in B without keeping A linked to B?
Thanks for your help.
This is because you are only pointing the reference to B, without making a copy at all. Instead do the following to actually create a copy.
A = [1,2,3]
B = A[:]
This will work if there are no referenced variables. If you don't want that behaviour, then use a deep_copy method as below
B = copy.deepcopy(A)
Then if you change A, it won't change B

Resolution of an append to an empty array [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 6 years ago.
I noticed something odd demonstrated below:
>>> print [].append(1)
None
>>> a = [].append(1)
>>> print a
None
>>> a = []
>>> a.append(1)
>>> print a
[1]
I do not understand what the difference between the first two statements and the last one is. Can anyone explain?
EDIT:
This question was asked poorly. I should also have noted that:
>>> print [] + [1]
[1]
Thank you for explaining that the return value for operations on mutable data-types in python is normally None.
The .append() method does not return a modified list as you would expect.
With this line a = [].append(1) you're assigning the return value (which is None in Python by default) to the variable a. You're not assigning an array that you've just appended to.
However, with this code
>>> a = []
>>> a.append(1)
you're modifying the array in the variable a.

Understanding Python variables assignment [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 9 years ago.
If I execute this code:
a = [1,2,3]
b = a
b.remove(2)
print(a,b)
What I expect to see is:
[1,2,3] [1,3]
But this is what I really get:
[1,3] [1,3]
Why calling b.remove(2) also affects a?
What if I want to change b,while keeping a copy of the original content in a?
When you do b = a, you simply create another reference to the same list. So any modifications to that list will affect both a and b. So doing b.remove(2) will affect the single list that you have.
If you want to get your expected results, you can create a copy of the list:
b = a[:]
This way, you create a copy of the list, and you can modify one without changing the other.
>>> a = [1,2,3]
>>> b = a[:]
>>> b.remove(2)
>>> print a,b
[1, 2, 3] [1, 3]
a and b are two names for the same list, so if you change the list through one name, you can see the changes through the other name. If you want them to be different lists, make a copy:
b = a[:]
or
b = list(a)

Categories