Mastering Python’s Zip Function: Key Use Cases

Zip is an in-built Function in Python. You use it to pair (zipping) lists. It is also useful for unzipping. And you can use it to iterate through multiple iterables. Additionally, to transpose a matrix. These examples help make the concept clearer.

Table of contents

Python Zip Function
Photo by Anna Shvets on Pexels.com

Pairing lists

One of the most common use cases for the zip() is to combine two or more lists element-wise. Using zip(), you can iterate over multiple lists together, creating a new iterable that pairs corresponding elements. We’ll show how it works with a piece of code.

Example 1

# Two lists of equal length
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 32, 28]

# Combine lists using zip()
combined = list(zip(names, ages))
# Iterate over the zipped iterable
for name, age in combined:
    print(name, age)

Output

Alice 25
Bob 32
Charlie 28
** Process exited - Return Code: 0 **
Press Enter to exit terminal
Advertisements

Unzipping

zip() combines(zips) iterables, also be used to unzip them. In this example, we’ll explore how to reverse the process and separate a zipped iterable back into individual lists. This technique can be handy when you need to analyze or manipulate data.

Example 2

# Zipped iterable
zipped = [('Alice', 25), ('Bob', 32), ('Charlie', 28)]
# Unzip the zipped iterable

names, ages = list(zip(*zipped))
# Print the separate lists
print(names)
print(ages)

Output

('Alice', 'Bob', 'Charlie')
(25, 32, 28)
** Process exited - Return Code: 0 **
Press Enter to exit terminal

Iterating (for-loop)

The zip() function offers a convenient way to iterate over multiple iterables simultaneously. Whether you have lists, tuples, or even strings, you can use zip() to traverse them in a synchronized manner. We’ll provide a practical example where zip() simplifies the process of working with data from different sources.

Example 3

# Three iterables of equal length
numbers = [1, 2, 3]
letters = ['A', 'B', 'C']
symbols = ['!', '@', '#']
# Iterate over multiple iterables using zip()
for num, letter, symbol in list(zip(numbers, letters, symbols)):
    print(num, letter, symbol)

Output

1 A !
2 B @
3 C #
** Process exited - Return Code: 0 **
Press Enter to exit terminal

Transposing a matrix

Example 4

# Matrix represented as a list of lists
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
# Transpose the matrix using zip()
transposed_matrix = list(zip(*matrix))

# Print the transposed matrix
for row in transposed_matrix:
    print(row)

Output

(1, 4, 7)
(2, 5, 8)
(3, 6, 9)
** Process exited - Return Code: 0 **
Press Enter to exit terminal

Conclusion

Python’s zip function is a powerful tool that enables developers to efficiently work with multiple iterables. It combines, unzips, iterates, and transposes to enhance productivity and expand possibilities in Python projects.

References:

Discover more from Srinimf

Subscribe now to keep reading and get access to the full archive.

Continue reading