Tutorials
python

Tuples in Python

Learn to use and unpack Tuples in Python.

Tuples are made of several items just like a list, but they cannot be modified in any way. It is very common for tuples to be used to represent data from a database. 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. However, you can also "unpack" the tuple into multiple variables such as type, count = ('chocolate chip cookies', 15) that will set type to 'chocolate chip cookies' and count to 15.

Often you'll want to pair up multiple array data types. The zip() function does just that. It will return a list of tuples containing one element from each list passed into zip().

When looping over a list, you can also track your position in the list by using the enumerate() function. The function returns the index of the list item you are currently on in the list and the list item itself.

You'll practice using the enumerate() and zip() functions in this exercise, in which your job is to pair up the most common boy and girl names. Two lists - girl_names and boy_names - have been pre-loaded into your workspace.

Exercise

TRY IT YOURSELF: Access the exercise in our Data Types for Data Science in Python here.

  • Use the zip() function to pair up girl_names and boy_names into a variable called pairs.
  • Use a for loop to loop through pairs, using enumerate() to keep track of your position. Unpack pairs into the variables idx and pair.
  • Inside the for loop:
    • Unpack pair into the variables girl_name and boy_name.
    • Print the rank, girl name, and boy name, in that order. The rank is contained in idx.
# Pair up the girl and boy names: pairs
pairs = ____

# Iterate over pairs
for ____, ____ in ____:
    # Unpack pair: girl_name, boy_name
    ____, ____ = ____
    # Print the rank and names associated with each rank
    print('Rank {}: {} and {}'.format(____, ____, ____))

Video

To learn more about unpacking 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.