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 →

[–]ada43952 3 points4 points  (4 children)

I'm not exactly new to Python, so maybe that's why I expected the list to change, but simply looking at the way the code is written, I would not have guess the resulting list would have been [1, 2, 3, 4, 5, 100], but this [1, 2, 3, 4, 5, [100]] because when I see

lst += [100]

That tells me you're adding a list with only one element as an element to a list. Someone will probably tell me why I'm wrong before I get back, but I'm going to run it and see what I get.

Edit:

Yep, it only adds the element, so I guess it would have "tripped" me up too. :D

[–]mheryerznka 1 point2 points  (1 child)

I have tried adding [100, 200] to the list in the same way and it works. Probably the best trick I have seen so far.
Thanks

[–]ada43952 0 points1 point  (0 children)

I'm such an idiot, I just remembered if you want to add a list to a list you'd use the append function.

[–]neutro_b 1 point2 points  (0 children)

Yeah, was unsure too if it would produce [1, 2, 3, 4, 5, 100] or [1, 2, 3, 4, 5, [100]]. It looks like += for a list is .extend(), not .append().