Python Variables

In Python, a tuple is a comma-separated sequence of values. Very similar to a list. However, there's an important difference between the two.

The main difference between tuples and lists is that lists are mutable and tuples are not. A mutable object is one that can be changed. An immutable object is one that contains a fixed value that cannot be changed. If it needs to be changed, a new object must be created.

Therefore, you'd generally store different data inside a tuple than you would a list. Lists typically contain items that are similar to each other, whereas tuples usually contain items that are diverse in type or character. However, there's no hard and fast rule to this. But if you want to be able to update the individual items, use a list. Otherwise, use a tuple.

Create a Tuple

Creating a tuple is just like creating a list, except that you use regular brackets instead of square brackets:

Actually, the brackets are optional. So you could also do this:

Here's an example of using both methods to set two tuples (one with brackets, one without), then printing both:

Result
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
('Saturday', 'Sunday')

Single Item Tuples

If, for some reason, you need to create a tuple with a single item, you need to include a comma, otherwise you'll end up with a string.

Here's an example to demonstrate this. We omit the comma in variable a and include it in variable b:

Result
<class 'str'>
<class 'tuple'>

So the variable without a comma ended up as a string. The variable with the comma became a tuple.

Tuple containing a List

Tuples can also contain lists. Example:

Result
('Banana', [1, 2, 3])

Access the Values in a Tuple

You can access the values in a tuple the same way you can with lists — by appending the variable name with an index value enclosed in square brackets. Like this:

Result
Tuesday

You can also pick a range of values by using two indices separated by a colon. Like this:

Result
('Tuesday', 'Wednesday', 'Thursday')

As with lists, this returns all items up to, but not including the second index specified (remembering that the count starts at zero).

Access a List Item within a Tuple

You can access a list item within a tuple by referring to the list's index within the tuple, followed by the list item's index within the list (each index surrounded by its own set of square brackets).

Like this:

Result
Tuesday

Update a Tuple

As mentioned, tuples contain fixed items and they're immutable, which means that they can't be altered. So you can't change "Wednesday" to "Humpday" or anything like that.

But you can reassign a tuple. That is, you can assign a completely different set of values to a tuple. Like this:

Result
('Saturday', 'Sunday')
('Sat', 'Sun')

Update a List Item within a Tuple

Although you can't change tuple items, you can change list items within a tuple. Here's an example of doing that:

Result
(101, 202, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])
(101, 202, ['Monday', 'Tuesday', 'Humpday', 'Thursday', 'Friday'])

Concatenate Tuples

You can also use a tuple as the basis for another tuple. For example, you can concatenate a tuple with another one to create a new a tuple that contains values from both tuples.

Result
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
('Saturday', 'Sunday')
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

As you can see, the two original tuples remain the same, while the third one contains all values from both of them.

Delete a Tuple

Although you can't delete individual tuple items, you can use the del keyword to delete the whole tuple. Like this:

Named Tuples

Sometimes it makes sense to create a named tuple. A named tuple enables you to reference each item by a name, rather than its index. This can help the readability of your code and it can do wonders for your debugging, as it's much easier to see which values are being referenced in a block of code.

Here's an example of creating, and referencing a named tuple:

Result
Individual(name='Homer', age=37, height=178)
Homer
37
178

So instead of using print(user[0]) to type out the name field, we can use print(user.name) instead. And of course, the same applies for all other fields.

You'll see that the first line imports a function called namedtuples from the collections module. We need this function before we can create our named tuple. I explain modules later in this tutorial, but for now, the main thing to understand is that you need to import that function before you can create a named tuple.