Join two Python tuples
December 22, 2020 ‐ 1 min read
Tuples in Python are immutable, meaning they can't be modified once they are created. Adding elements or reordering elements is a no-no. So methods like
.sort() which you can use on lists are not available on tuples.
Meaning that in order to add elements to a tuple you need to merge two tuples together by using the
toppings = ('tuna', 'cheese', 'salami', 'pepper') toppings += ('tomato',) print(toppings) # => ('tuna', 'cheese', 'salami', 'pepper', 'tomato')
Pay attention to the trailing comma in
('tomato',), without the comma this would be evaluated as a string.