zip()
is a built-in Python function that takes two or more iterable objects and returns an iterator that aggregates the items from each iterable object. In simple terms, zip()
pairs up the elements of multiple lists, tuples, or any other iterable objects.
The syntax for zip()
is:
zip(*iterables)
Here, *iterables
means that we can pass any number of iterable objects as arguments to the zip()
function.
How to Use Zip in Python?
Now that we have a basic understanding of what zip()
is, let’s see how we can use it in Python. Here are some examples:
Example 1: Zip Two Lists
fruits = ['apple', 'banana', 'orange']
quantities = [10, 20, 30]
for fruit, quantity in zip(fruits, quantities):
print(fruit, quantity)
Output:
apple 10
banana 20
orange 30
In this example, we have two lists fruits
and quantities
. We use zip()
to pair up the elements of both lists and then loop through them using a for
loop. In each iteration, we print the corresponding element from both lists.
Example 2: Zip Three Lists
fruits = ['apple', 'banana', 'orange']
quantities = [10, 20, 30]
prices = [1.0, 2.0, 3.0]
for fruit, quantity, price in zip(fruits, quantities, prices):
total_cost = quantity * price
print(fruit, quantity, price, total_cost)
Output:
apple 10 1.0 10.0
banana 20 2.0 40.0
orange 30 3.0 90.0
In this example, we have three lists fruits
, quantities
, and prices
. We use zip()
to pair up the elements of all three lists and then loop through them using a for
loop. In each iteration, we calculate the total cost of each fruit and print all the details.
Example 3: Zip Two Tuples
person1 = ('John', 25, 'Male')
person2 = ('Jane', 30, 'Female')
for (name1, age1, gender1), (name2, age2, gender2) in zip([person1], [person2]):
print(name1, age1, gender1)
print(name2, age2, gender2)
Output:
John 25 Male
Jane 30 Female
In this example, we have two tuples person1
and person2
. We use zip()
to pair up the elements of both tuples and then loop through them using a for
loop. We then print the details of each person.
Example 4: Unzip a List of Tuples
fruits = [('apple', 10), ('banana', 20), ('orange', 30)]
names, quantities = zip(*fruits)
print(names)
print(quantities)
Output:
('apple', 'banana', 'orange')
(10, 20, 30)
In this example, we have a list of tuples fruits
. We use zip()
to pair up the elements of each tuple and then use the * operator to unzip the pairs into two separate tuples names and quantities. We then print both tuples.
Example 5: Zip with Different Lengths
fruits = ['apple', 'banana', 'orange']
quantities = [10, 20]
for fruit, quantity in zip(fruits, quantities):
print(fruit, quantity)
Output:
apple 10
banana 20
In this example, we have two lists fruits
and quantities
. However, the quantities
list has only two elements compared to three in the fruits
list. When we use zip()
in this case, it pairs up only the first two elements from both lists and discards the third element from the fruits
list.
Conclusion
In this article, we discussed what zip()
is, how to use it in Python, and provided some examples to help you understand its usage. zip()
is a very powerful function that can be used to pair up elements from multiple iterable objects. It can be used in various scenarios such as data analysis, machine learning, and more.