1

Ok, so here's the python code:

example = [1,2,3]
a = example
b = example.append(4)

print(a)
print(b)

And the output is:

[1, 2, 3, 4]
None

while I was expecting something like:

[1,2,3]
[1,2,3,4]

why does "a" gets modified even when i was only modifying "example"

and why does "b" return "None"

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
applepie
  • 51
  • 1
  • 2
  • 1
    You didn't make a copy, you created an additional reference. – Martijn Pieters Dec 05 '15 at 22:55
  • 1
    In-place operations always return `None`. The original list is altered, no need to return another reference. See [why does python's list.append evaluate to false?](http://stackoverflow.com/q/1682567) – Martijn Pieters Dec 05 '15 at 22:56
  • `b` is None because the return value of `append` is None. That's a common idiom for functions that modify an object. – Mark Ransom Dec 05 '15 at 22:56
  • is there a way how i can COPY the list into another variable e.g in this case i would like to make a copy of "example" into "a" – applepie Dec 06 '15 at 00:10

0 Answers0