Solved
append() vs extend() — added a list inside my list
items = [1, 2]
items.append([3, 4])
print(items) # [1, 2, [3, 4]]I wanted [1, 2, 3, 4].
items = [1, 2]
items.append([3, 4])
print(items) # [1, 2, [3, 4]]I wanted [1, 2, 3, 4].
Comments 4
Join the discussion CommentAlready have an account? Log in
appendadds its argument as one element.extendadds each element of an iterable:items += [3, 4]does the same as extend.extend, of course. Thanks!
Watch out:
items.extend("abc")adds 'a', 'b', 'c' separately, because a string is iterable.pop() is also nice for queues, but
pop(0)is slow on big lists.collections.dequeis made for that.