Get Started

Using zip

Another interesting function that you've learned is zip(), which takes any number of iterables and returns a zip object that is an iterator of tuples. If you wanted to print the values of a zip object, you can convert it into a list and then print it. Printing just a zip object will not return the values unless you unpack it first. In this exercise, you will explore this for yourself.

Three lists of strings are pre-loaded: mutants, aliases, and powers. First, you will use list() and zip() on these lists to generate a list of tuples. Then, you will create a zip object using zip(). Finally, you will unpack this zip object in a for loop to print the values in each tuple. Observe the different output generated by printing the list of tuples, then the zip object, and finally, the tuple values in the for loop.

This is a part of the course

“Python Toolbox”

View Course

Exercise instructions

  • Using zip() with list(), create a list of tuples from the three lists mutants, aliases, and powers (in that order) and assign the result to mutant_data.
  • Using zip(), create a zip object called mutant_zip from the three lists mutants, aliases, and powers.
  • Complete the for loop by unpacking the zip object you created and printing the tuple values. Use value1, value2, value3 for the values from each of mutants, aliases, and powers, in that order.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a list of tuples: mutant_data
mutant_data = ____

# Print the list of tuples
print(mutant_data)

# Create a zip object using the three lists: mutant_zip
mutant_zip = ____

# Print the zip object
print(mutant_zip)

# Unpack the zip object and print the tuple values
for ____ in ____:
    print(value1, value2, value3)

This exercise is part of the course

Python Toolbox

IntermediateSkill Level
4.5+
104 reviews

Continue to build your modern Data Science skills by learning about iterators and list comprehensions.

You'll learn all about iterators and iterables, which you have already worked with when writing for loops. You'll learn some handy functions that will allow you to effectively work with iterators. And you’ll finish the chapter with a use case that is pertinent to the world of data science and dealing with large amounts of data—in this case, data from Twitter that you will load in chunks using iterators.

Exercise 1: Introduction to iteratorsExercise 2: Iterators vs. IterablesExercise 3: Iterating over iterables (1)Exercise 4: Iterating over iterables (2)Exercise 5: Iterators as function argumentsExercise 6: Playing with iteratorsExercise 7: Using enumerateExercise 8: Using zip
Exercise 9: Using * and zip to 'unzip'Exercise 10: Using iterators to load large files into memoryExercise 11: Processing large amounts of Twitter dataExercise 12: Extracting information for large amounts of Twitter dataExercise 13: Congratulations!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free