Zip Lists in Python (Example)

In this tutorial, you’ll learn how to zip lists in Python. Zipping lists allows you to iterate over multiple lists simultaneously and perform operations on corresponding combined elements.

The table of contents is structured as follows:

Let’s get started!

Initializing Sample Lists

Let’s assume we have two lists of equal length, and we want to pair elements together from these lists:

# Initializing sample lists
list1 = [1, 2, 3]
list2 = ['A', 'B', 'C']

This creates two lists, list1 and list2, with some elements.

Example: Zip Lists

The most straightforward way to do this in Python is by using the built-in function zip().

# Zip lists
pairs = zip(list1, list2)

In this example, we use the zip() function to pair corresponding elements from list1 and list2. The zip() function returns an iterator, but we typically want our result as a list, which is a more user-friendly data structure.

The resulting pairs can be converted into a list using the list() function.

# Zip lists
paired_list = list(zip(list1, list2))

Let’s print the paired list now!

print(paired_list)
# [(1, 'A'), (2, 'B'), (3, 'C')]

The output shows the paired lists where each element in list1 is paired with the corresponding element in list2. For more advanced methods of pairing lists, see my tutorial: Return All Possible Pairs of List Elements in Python.

Video, Further Resources & Summary

In this tutorial, we explored pairing lists using the zip() function in Python.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top