Tuples are made of several items, just like a list, but they cannot be modified in any way. They are widely used internally in many of the systems we depend on, like databases. Tuples hold data in the order we supply it, & we can access the elements inside a tuple with an index.

For example, if we have a tuple like ('chocolate chip cookies', 15) & we want to access each part of the data, we can use an index just like a list. Tuples are easier to process & more memory efficient than lists.

They are immutable, which means we cannot add or remove elements from them. This property of tuple is important because we can use them to ensure that our data is not altered. We can create tuples by pairing up elements.



Here we have a list of most popular cookies in the US & India, & we want to build a list of pairs by the popularity rank of the cookie in each country, & we will pass them to the zip function. Then we print the result of the zip, & we get what looks like a list of Tuples.

We have two lists: us_cookies&in_cookies.

Notice that the tuples use parenthesis as their object representation.



The below syntax allows us to create a more readable & less error-prone code. Here we have a tuple containing top-ranked cookie from two countries, & we want to store them as us_num_1&in_num_1 so that we can print them name by name.

We start by putting both variables as the target of the assignment statement separated by a comma. Then assign the first tuple in our top_pairs list to them.


Here we build a for loop that uses tuple unpacking when iterating over the top_pairs list. It splits each tuple in the list into its Indian & US cookie elements. We then use each of these variables to print the cookies in order.

Enumerating Positions


Here we enumerate our top pairs list & split that resulting tuple into index idx & item.


Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store