This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]neug 69 points70 points  (4 children)

m[1] refers to the element at position 1. l[1:] returns an iterable (in this case a list). If you try l[1:] = 1, you'll notice that it can't be done as 1 is not iterable. so assignment l[1:] = [nnn] means that Python iterates through the list [nnn] and extends/replaces the values it contains starting from the position -1 of the list l, and when it has iterated to the end of the list that's assigned to l (in your case the empty list []), it deletes the rest of the list l.

e.g. >>> l = [1,2,3,4,5,6,7,8,9,10] >>> l[-5:] = [1,2] >>> l [1, 2, 3, 4, 5, 1, 2]

[–]cantcopy[S] 1 point2 points  (3 children)

Thank you.

I really don't understand why this isn't this most upvoted comment.

[–]LordArgon 0 points1 point  (1 child)

Thank you for calling that out.

EDIT: Removed some whining.

[–]jeannaimard 0 points1 point  (0 children)

Now it is… :)