Skip to main content
HomeAbout PythonLearn Python

Tuples in Python Tutorial

Learn to use and unpack Tuples in Python.
Jun 2020  · 3 min read

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, and we can access the elements inside a tuple with an index.

For example, if you have a tuple like ('chocolate chip cookies', 15) and you want to access each part of the data, you can use an index just like a list. Tuples are easier to process and 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.

Zipping

Often, we will have lists where we want to match up elements into pairs, and the zip function enables us to do that. Tuples are commonly created by zipping lists together with zip().

Example

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

We have two lists: us_cookies, in_cookies.

tuples

Notice that the tuples use parenthesis as their object representation.

Unpacking

Tuple unpacking, also sometimes called tuple expansion, allows us to assign the elements of a tuple to named variables for later use. Unpacking tuples is a very expressive way of working with data.

Example

The below syntax allows you to create a more readable and less error-prone code. Here we have a tuple containing top-ranked cookie from two countries, and we want to store them as us_num_1 and 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.

example

Example

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 and US cookie elements. We then use each of these variables to print the cookies in order.

example

Enumerating Positions

Another useful tuple creation method is the enumerate() function. Enumeration is used in loops to return the position and the data in that position while looping.

Example

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

example

To learn more about tuples in Python, please see this video from our course Data Types for Data Science in Python.

This content is taken from DataCamp’s Data Types for Data Science in Python course by Jason Myers.

Check out our Python Data Structures Tutorial.

Python courses

Introduction to Python

BeginnerSkill Level
4 hr
5.1M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

How to Learn Python From Scratch in 2023: An Expert Guide

Discover how to learn Python, its applications, and the demand for Python skills. Start your Python journey today ​​with our comprehensive guide.
Matt Crabtree's photo

Matt Crabtree

19 min

10 Essential Python Skills All Data Scientists Should Master

All data scientists need expertise in Python, but which skills are the most important for them to master? Find out the ten most vital Python skills in the latest rundown.

Thaylise Nakamoto

9 min

Benchmarking High-Performance pandas Alternatives

Discover the latest benchmarking of Python's powerful pandas alternatives, Polars, Vaex, and Datatable. Discover their performance in data loading, grouping, sorting, and more.
Zoumana Keita 's photo

Zoumana Keita

13 min

Distributed Processing using Ray framework in Python

Unlocking the Power of Scalable Distributed Systems: A Guide to Ray Framework in Python
Moez Ali's photo

Moez Ali

11 min

Geocoding for Data Scientists: An Introduction With Examples

In this tutorial, you will learn three different ways to convert an address into latitude and longitude using Geopy.
Eugenia Anello's photo

Eugenia Anello

9 min

A Complete Guide to Socket Programming in Python

Learn the fundamentals of socket programming in Python
Serhii Orlivskyi's photo

Serhii Orlivskyi

41 min

See MoreSee More