+ 4
Is x[:] a special syntax for copy an array in python?
please take a look at the attached code which I failed on challenge. https://code.sololearn.com/c2DKYnh40dZ3/?ref=app
2 Answers
+ 6
Given a list y, writing x=y will make the lists identical, and writing x=y[:] will make the lists equal.
Identity and equality are not the same thing. If you write x=y and then change y, youâll be also changing x, because theyâre identical and that means they are the one and same list. However, if you write x=y[:] and then change y, x will not be changed, since theyâre different objects (even thou they have once been equal).
Also, x=list(y) will do the same as x=y[:].
The problem on your code is that, by appending one list to another, you make a new item in one list that is equal to the other, and end up with a listception (a list inside a list).
You can write a for loop to append term by term, or (which is better) simply concatenate the two lists, by writing new_list=list1+list2. Note that summing lists will concatenate them, not sum their values. Try the following:
x = [1, 2, 3]
y = x + x
print(y)
+ 2
Yes. It means first index to last index. ***Note that all values in list are included. Not like , for eg. [0:4] which not include value at index 4. Only values at index 0 to 3 are included.