Merge multiple Python lists into one
October 4, 2020 ‐ 1 min read
Merging Python lists together is as easy as using the
orders = ['cheeseburger'] + ['soda'] print(orders) # => ['cheeseburger', 'soda']
You can leverage the
extend() function for this purpose as well. You call it on the list you want to extend and pass it a list.
orders.extend(['fries']) print(orders) # => ['cheeseburger', 'soda', 'fries']
Be aware that you pass it a list, not just a string. If you pass it just a string all the characters get inserted individually.
orders.extend('fries') print(orders) # => ['cheeseburger', 'soda', 'f', 'r', 'i', 'e', 's']