~3 min read|
Unlike a list or a dictionary, in Python, tuples are immutable.1 This is clear when investigating the methods available on the
tuple object. Conspicuously absent are any methods for adding or removing elements:
>>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
So, how can you add a new item to a tuple? Technically you can’t. But what you can do is reassign the variable to a new tuple.
Let’s look at an example of a family.
>>> family = 'Stephen', 'Kate', 'Finn' # packing a tuple >>> *adults, animals = family # unpacking >>> adults ['Stephen', 'Kate'] >>> animals 'Finn' >>> id(family) 4326772672
Below, the family gets a new addition, a cat named Garfield.
>>> family = *family, 'Garfield' >>> family ('Stephen', 'Kate', 'Finn', 'Garfield') >>> id(family) 4326761632
The first thing we do is create a new tuple. This is done by unpacking the original tuple and then adding a new member (
Garfield). We know it’s a new tuple because the id that Python’s tracking for its position in memory is different.
Let’s split this group into a few different factions:
>>> *original_cast, new_addition = family >>> original_cast ['Stephen', 'Kate', 'Finn'] >>> new_addition 'Garfield' >>> *adults, dog, cat = family >>> adults ['Stephen', 'Kate'] >>> dog 'Finn' >>> cat 'Garfield' >>> dad, mom, *animals = family >>> dad 'Stephen' >>> mom 'Kate' >>> animals ['Finn', 'Garfield']
These examples better show the various ways a tuple can be unpacked.
In case you missed it, packing is the term used to describe the creation of the tuple. E.g.,:
my_tuple = ('a', 'b', 'c') # this is packing my_other_tuple = 1, 2, 3 # this is also packing
Tuples may be immutable, but with the
spread unpacking operator, it’s easy enough to modify them. Just remember that you’re creating a brand new list - with all of the performance implications therein.
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!