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.

Unlock This Lesson for Free - No Payment Required!

If you like to keep on reading, register now!

  • Learn CCNA, CCNP and CCIE R&S. Explained As Simple As Possible.
  • Get Instant Access to this Full Lesson, Completely for Free!
  • Unlock More to Read. More Lessons Added Every Week!
  • Content created by Rene Molenaar (CCIE #41726)
1826 Sign Ups in the last 30 days

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