Python tuples are a data structure that store an ordered sequence of values. Tuples are immutable. This means you cannot change the values in a tuple. Tuples are defined with parenthesis.
Tuples are a core data structure in Python. They let you store an ordered sequence of items. For example, you may use a tuple to store a list of employee names. You could use a tuple to store a list of ice cream flavors stocked at an ice cream shop.
In this tutorial, we are going to break down the basics of the tuple data type. We’ll discuss its purpose and provide examples to demonstrate how you can work with this data type.
Understanding Python Tuples
Tuples are immutable, ordered lists of data, unlike lists. Lists are mutable, which means you can change the contents of a list. Individual values in a tuple are called items. Tuples can store any data type.
A tuple is a comma-separated sequence of items. This sequence is surrounded by parenthesis (()). Let’s create a tuple:
ice_cream_flavors = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')
When we print our tuple to the console using the print()function, we will see the tuple that we originally declared. The values in our tuple are separated by commas:
Our code returns the following:
('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')
Tuples store data with a common theme, similar to lists. In the above example, our tuple stores a collection of ice cream flavors. It could also store a list of student grades or a list of phones sold by an electronics store, for instance.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Tuples are similar to Python lists, with one big difference: you cannot modify a tuple. You should only use tuples when you want a list to remain the same. If we wanted to add flavors to our ice cream flavors list above, a regular list would likely be better. This is because we could change the contents of the list as our ice cream flavors change.
How to Access Tuple Items
Each item in a tuple has a unique index value, starting with zero. Index values continue in increments of one. You can access an individual item in a tuple by referencing the item’s index value.
Here are the index values for the ice_cream_flavors tuple that we declared above:
Now that we know the index values for each item, we can access a single element individually. The following code allows us to get the item at the index value 3:
Our code returns: Strawberry. Strawberry is the item whose index value is 3.
Each item in a tuple has a negative index value. These values let us count backward from the end of a tuple. They start at -1. Using a negative index number may be more convenient if you are working with a long list. This is because you can work backwards from the end of the list.
Here are the negative index values for our ice_cream_flavors tuple:
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
If we wanted to get the value at the index position of
-1, we could use the following code:
Our code returns:
Similarly, if we want to get a range of items within our tuple, we can specify a range of indexes to retrieve. In order to do so, we need to specify where to start and end our range. We can use the following code to retrieve every item in the range of the 1 and 4 index values:
Our code returns: (‘Vanilla’, ‘Mint’, ‘Strawberry’)
In this example, our code returns every value with an index value between 1 and 4, exclusive of the last index value. Our code does not return Choc-Chip because Choc-Chip does not appear in the defined range.
If we wanted to retrieve items from either end of the list, we can remove the first number in our range. So, if we wanted to get the first two items in our list, we could use the following Python program:
Our code returns:
Slicing with Negative Index Numbers
In addition, you can use negative index numbers when you’re slicing tuples. The below example returns the last two items in our list:
Our code returns:
There is also a feature of tuple slicing called stride. This feature allows us to skip over items after the first item is retrieved from a tuple. If we want to use stride, we can add a value to the end of our slicing function. This states how many items the list should skip over between increments.
So if we wanted to get every second number in our tuple, we could use the following code:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Our code returns:
('Chocolate', 'Mint', 'Choc-Chip')
The first number in our slice (0) refers to when the slice should start in the array. 5 refers to the final item our slice should reference, exclusive of that item. The third number (2) refers to the stride we want to use. Stride represents how many values after the first item the code should skip over when slicing a tuple.
You can use operators to concatenate (merge) or multiply the contents of a tuple. Additionally, the + operator can be used to concatenate two or more together.
Let’s say that we have a list of ice cream flavors. We want to add the experimental flavors we have been piloting to the main list of flavors. We could use the following code to merge our tuples:
ice_cream_flavors = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip') experimental_flavors = ('Cookie Dough', 'Rocky Road', 'Mint Chocolate Chip') new_menu = ice_cream_flavors + experimental_flavors print(new_menu)
Our code returns the following:
('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip', 'Cookie Dough','Rocky Road', 'Mint Chocolate Chip')
As you can see, we now have a new tuple that contains values from both lists. However, because a tuple cannot be modified, we created a new one called new_menu.
Iterating Through a Tuple
Like lists, you can iterate through a tuple value in Python. So, if you had a tuple of ice cream flavors that you wanted to print individually, you could do so.
Here’s an example of a for loop being used to iterate through our ice cream flavors tuple and prints each value:
ice_cream_flavors = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip') for f in ice_cream_flavors: print(f)
The result of our code is as follows:
Chocolate Vanilla Mint Strawberry Choc-Chip
You can check if an item exists in a tuple by using an if…in statement. An if…in statement checks whether a value exists in a collection. Here’s an example of an if…in statement being used to check if our ice cream parlor sells Mint ice cream:
ice_cream_flavors = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip') if "Mint" in ice_cream_flavors: print("Yes, we sell mint ice cream!")
Our code returns: Yes, we sell mint ice cream! This is because “Mint” is in our list.
Lists vs. Tuples
Unlike lists, tuples cannot be modified. You cannot add, remove, or replace an item within a tuple. Using a list may be more appropriate if you intend to modify the values you want to store.
That said, you can concatenate two or more tuples, which means that you can combine two tuples to form a new one.
To modify the contents of a tuple, we need to convert it into a list. Then, we can convert our list back into a tuple. Here’s how we could convert our new list of flavors to a list that we can modify:
Our code returns a list. We can use the tuple() method to convert our value back to a tuple:
Our menu is now stored as a tuple. We know this because our collection of items is surrounded by curly brackets. Curly brackets denote a tuple.
The tuple data type is an immutable, ordered data type that allows you to store data in Python. Tuples are somewhat faster to use than lists in Python because they cannot be changed. As such, they’re useful if you need to store data that will not change.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.