Combining Elements In List: Seems Like Python Treats The Same Item In Two Different Ways And I Don't Know Why
I'm working my way through CodeAcademy and I have a question that's going unanswered there. The assignment is to take a list of lists and make a single list of all its elements. Th
Solution 1:
The +=
in-place add operator on a list does the same thing as calling list.extend()
on new_list
. .extend()
takes an iterable and adds each and every element to the list.
list.append()
on the other hand, adds a single item to the list.
>>>lst = []>>>lst.extend([1, 2, 3])>>>lst
[1, 2, 3]
>>>lst.append([1, 2, 3])>>>lst
[1, 2, 3, [1, 2, 3]]
Post a Comment for "Combining Elements In List: Seems Like Python Treats The Same Item In Two Different Ways And I Don't Know Why"