How to modify Tuples in Python. The Work Arounds.

Alwaz Qazi
Nerd For Tech
Published in
3 min readJul 29, 2021

Just like List and dictionaries Tuple is another data type in python which is used to store multiple items in a single variable. But the thing that makes it different from lists and dictionaries is Tuples are “immutable”.

Lets understand the difference between list and tuple with an example.

list is declared using [] brackets as shown below.

whereas Tuple is declared using () bracket or by using tuple() method as shown below.

The main difference between a Tuple and a List is we can modify list by adding, removing items from it, but to a Tuple we cannot change it, by adding or taking things out from it.

For example.

This is a list named ‘groceries’ initially having three items in it. Now, I modified this list by adding another item “cheese” to it and printed that out.

As you can see the final list having cheese in it as well.

But we cannot do that with tuples.

Why do we even need a tuple ?

When we are working with data we don’t want to change such as, storing people’s passwords, we store them in tuples because this is something you never want to change.

But what if user wants to change their password ?

That will be stored in another tuple, but the old password will remain unchanged.

The workarounds.

Since, tuples are immutable data types and cannot be changed but there are somme work arounds to modify tuple.

Create a Tuple.

Convert it into a list

Modify the list.

Convert it back into Tuple.

By doing this you can easily add, remove, update items of a Tuple.

--

--