Python 101 – Learning About Tuples

Tuples are another sequence type in Python. Tuples consist of a number of values that are separated by commas. A tuple is immutable whereas a list is not. Immutable means that the tuple has a fixed value and cannot change. You cannot add, delete or modify items in a tuple. Immutable objects are useful when you need a constant hash value. The most popular example is the key to a Python dictionary.

In this article, you will learn how to:

  • Create tuples
  • Work with tuples
  • Concatenate tuples
  • Special case tuples

Let’s find out how to create tuples!

Creating Tuples

You can create tuples in several different ways. Let’s take a look:

>>> a_tuple = (4, 5)
>>> type(a_tuple)
<type 'tuple'>

One of the simplest methods of creating a tuple is to use plain parentheses: (). The items in the parentheses are a sequence of values. They could be integers, lists, dictionaries or any other object.

You can also cast a list into a tuple using the tuple() function:

>>> a_tuple = tuple(['1', '2', '3'])
>>> type(a_tuple)
<type 'tuple'>

This example demonstrates how to convert or cast a Python list into a tuple.

This last bit of code shows how to create a tuple implicitly:

>>> a_tuple = 2, 3, 4
>>> type(a_tuple)
<type 'tuple'>

If you have a sequence of objects (integers, strings, etc) and assign them to a single variable, Python will automatically turn them into a tuple for you.

Working With Tuples

Tuples actually don’t have a lot of ways to work with them due to the fact that they are immutable. If you were you run dir(tuple()), you would find that tuples have only two methods:

  • count()
  • index()

You can use count() to find out how many elements match the value that you pass in:

>>> a_tuple = (1, 2, 3, 3)
>>> a_tuple.count(3)
2

In this case, you can find out how many times the integer `3` appears in the `tuple`.

You can use `index()` to find the first index of a value:

>>> a_tuple = (1, 2, 3, 3)
>>> a_tuple.index(2)
1

This example shows you that the number 2 is at index 1, which is the second item in the tuple. Tuples are zero-indexed, meaning that the first element starts at zero.

You can use the indexing methodology that you learned about in the previous chapter to access elements within a tuple:

>>> a_tuple = (1, 2, 3, 3)
>>> a_tuple[2]
3

The first “3” in the tuple is at index 2, in other words.

Let’s try to modify an element in your tuple:

>>> a_tuple[0] = 8
Traceback (most recent call last):
    Python Shell, prompt 92, line 1
TypeError: 'tuple' object does not support item assignment

Here you try to set the first element in the tuple to 8. However, this causes a TypeError to be raised because tuples are immutable and cannot be changed.

Concatenating Tuples

Tuples can be concatenated together. However, when you do that, you will end up creating a new tuple:

>>> a_tuple = (1, 2, 3, 3)
>>> id(a_tuple)
140617302751760
>>> a_tuple = a_tuple + (6, 7)
>>> id(a_tuple)
140617282270944

Here you concatenate a second tuple to the first tuple. You can use Python’s id() function to see that the variable, a_tuple has changed. The id() method returns the id of the object. This is equivalent to a memory address like the ones that you might work within C++. You will notice that the number has changed after concatenating the second tuple. That means that you have a new object.

Special Case Tuples

There are two special-case tuples. A tuple with zero items and a tuple with one item. The reason they are special cases is that the syntax to create them is a little different.

To create an empty tuple, you can do one of the following:

>>> empty = tuple()
>>> len(empty)
0
>>> type(empty)
<type 'tuple'>
>>> also_empty = ()
>>> len(also_empty)
0

You can create an empty tuple by calling the tuple() function with no arguments or via assignment when using an empty pair of parentheses.

Now let’s create a tuple with a single element:

>>> single = 2,
>>> len(single)
1
>>> type(single)
<type 'tuple'>

To create a tuple with a single element, you can assign a value with a following comma. Note the trailing comma after the 2 in the example above.

Wrapping Up

The tuple is a fundamental data type in Python. It is used quite often in the language and is certainly one that you should be familiar with. In this chapter, you learned how to create a tuple in three different ways.

You also learned that tuples are immutable. Finally, you learned how to concatenate tuples and create empty tuples. Now you are ready to move on to the next chapter and learn all about dictionaries!