How To Zip Three Lists In Python

Python provides a built-in function called zip() that allows you to combine elements from multiple lists into tuples. This is especially useful when you have several lists of related data and want to process them together in a parallel manner.

In this blog post, we’ll learn how to use the zip() function to combine three lists in Python. We’ll also cover some common use cases and provide examples for better understanding.

The zip() Function

The zip() function in Python takes two or more iterable arguments (e.g., lists, tuples, or strings) and combines their elements into tuples. The output is an iterable object which can be converted to a list or tuple. The resulting tuples consist of elements at the same index from each input iterable.

The syntax for the zip() function is as follows:

zip(iterable1, iterable2, iterable3, ...)

Let’s see a simple example of how to zip three lists using the zip() function:

# Define three lists
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = [10, 20, 30]

# Zip the three lists
zipped = zip(list1, list2, list3)

# Convert the zipped object to a list
zipped_list = list(zipped)

# Print the zipped list
print(zipped_list)  # Output: [(1, 'a', 10), (2, 'b', 20), (3, 'c', 30)]

Use Cases and Examples

Now that we know how to zip three lists using the zip() function, let’s explore some common use cases and scenarios where this function can be beneficial.

1. Combining data from multiple sources

Suppose you have three lists containing data about employees – their ids, names, and salaries. You can use the zip() function to combine these lists into a single list of tuples containing the related data for each employee.

employee_ids = [101, 102, 103]
employee_names = ['Alice', 'Bob', 'Charlie']
employee_salaries = [50000, 60000, 70000]

employee_data = list(zip(employee_ids, employee_names, employee_salaries))
print(employee_data)
# Output: [(101, 'Alice', 50000), (102, 'Bob', 60000), (103, 'Charlie', 70000)]

2. Creating dictionaries from multiple lists

You can use the zip() function to create a dictionary with keys and values from separate lists. For example, if you have a list of countries and their capitals, you can create a dictionary that maps each country to its capital.

countries = ['USA', 'UK', 'Canada']
capitals = ['Washington D.C.', 'London', 'Ottawa']

country_capital_map = dict(zip(countries, capitals))
print(country_capital_map)
# Output: {'USA': 'Washington D.C.', 'UK': 'London', 'Canada': 'Ottawa'}

3. Iterating over multiple lists with a for loop

You can use the zip() function to iterate over multiple lists in a single for loop. This is helpful when you need to process elements from different lists together.

names = ['Alice', 'Bob', 'Charlie']
ages = [30, 25, 35]
scores = [100, 85, 95]

for name, age, score in zip(names, ages, scores):
    print(f"{name} is {age} years old and has a score of {score}.")

# Output:
# Alice is 30 years old and has a score of 100.
# Bob is 25 years old and has a score of 85.
# Charlie is 35 years old and has a score of 95.

In conclusion, the zip() function is a powerful tool that allows you to combine elements from multiple lists into tuples. By understanding how to use this function effectively, you can simplify your code and make it easier to work with related data across multiple lists.