x = [1,2,3,4,5] x.append(x[:]) print(len(x)) # output:6 # i was thinking the len should be 10 why 6? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

x = [1,2,3,4,5] x.append(x[:]) print(len(x)) # output:6 # i was thinking the len should be 10 why 6?

Python

8th Oct 2021, 11:20 AM
Khalif Baby👶
Khalif Baby👶 - avatar
3 Answers
+ 4
<x> will be [ 1, 2, 3, 4, 5, [ 1, 2, 3, 4, 5 ] ] 5 original items, and 1 list object
8th Oct 2021, 11:24 AM
Ipang
+ 2
x[ : ] means to create a new list object, copy all items from <x>. But it is a different object, not a reference copy. That is why the 6-th element is a list object (a copy of <x>) There's a chapter in the tutorial https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2453/
8th Oct 2021, 11:30 AM
Ipang
+ 1
Ipang please explain well give me details
8th Oct 2021, 11:25 AM
Khalif Baby👶
Khalif Baby👶 - avatar