Python Tuples

A tuple is like a list, but once created, you can’t edit it. A tuple is immutable. You create a tuple by adding items between parentheses (). Here is an example where we create two tuples:

We can access items in the tuple by specifying the index number in brackets [] behind the tuple:

Adding or removing items in a tuple

A tuple is immutable so you can’t modify it like a list. For example, try adding something to the tuple:

It’s a tuple, not a list, so the append attribute doesn’t exist.  You also can’t delete anything from the tuple:

The tuple doesn’t have a pop attribute to remove items.

You could use a tuple to “write protect” the contents of the tuple since you can’t modify it. However, it is possible to re-declare a tuple. Here is an example:

Above, I re-declared the variable “hostnames” with a new tuple. This is no problem in Python so the “write protect” analogy doesn’t make much sense.

Tuple vs List

Why would you want to use a tuple instead of a list?

To be honest, for us network engineers it doesn’t matter much.

When you have huge data sets, apparently a tuple is faster than a list. A tuple also requires less memory than a list. This is an issue that computer scientists might run into. As network engineers, we typically use Python for small scripts or to communicate with APIs. It’s unlikely that you run into any Python performance issues where the difference between a tuple or list is a key factor.


You have now learned what Python tuples are:

  • Tuples are similar to lists, but you can’t modify them.
  • It is possible to re-declare a variable that contains a tuple, though.
  • Theoretically, tuples are faster than lists but it’s unlikely as a network engineer to run into a performance issue like this.

Forum Replies

  1. Hi Rene,

    What does it mean that You could use a tuple to “write protect” the contents of the tuple since you can’t modify it.

  2. Hello Pradyumna

    One of the characteristics of a tuple data type as opposed to a list is that it is immutable. This means that the value stored within the variable cannot be changed. For example, if you try to change a value within the tuple, it will return an error, as shown in the lesson.

    Since the values stored within a tuple cannot be changed, we can say that they are “write protected” in much the same way as a write protected file cannot be changed. In other words, if you use a tuple in your programming, you can be sure that even an error in coding canno

    ... Continue reading in our forum

Ask a question or join the discussion by visiting our Community Forum