a = [1,2,3]
a = a + [a]
Print(a)
>>> [1,2,3,1,2,3]
a = [1,2,3]
a.append[a]
Print (a)
>>>[1,2,3,[...]]
Asked
Active
Viewed 60 times
-5

Mehrdad Pedramfar
- 10,941
- 4
- 38
- 59

Amitabh sahoo
- 3
- 1
-
Thanks for the clarification guys!! – Amitabh sahoo Jan 22 '19 at 17:15
2 Answers
1
Because [a]
creates another list and add it to a, but a.append(a)
appends the same list to itself and it will be cyclic, look at the example:
In [19]: id(a)
Out[19]: 139994593696008
In [20]: id([a])
Out[20]: 139994605200520

Mehrdad Pedramfar
- 10,941
- 4
- 38
- 59
1
because +
creates a new object and append
just appends to the original object. if you append a list to itself there is a cycle.
>>> a = [12]
>>> id(a)
4337923136
>>> a = a + [a]
>>> id(a)
4338091360
>>> a
[12, [12]]
>>> a.append(a)
>>> id(a)
4338091360
>>> a
[12, [12], [...]]
>>>

Pratik Deoghare
- 35,497
- 30
- 100
- 146