How to zip two lists in Python? To zip two lists into a list of tuples, use the zip()
function which takes iterable objects (can be zero or more) as arguments, aggregates them in a tuple and returns it as a list of tuples.
Related: Zip Dictionaries in Python
Besides zip() function you can also use the itertools.zip_longest() methods to zip two lists. We can use these methods directly and also with for loop and enumerate(). In this article, we will discuss different ways to zip two lists in python with examples.
1. Quick Examples of zipping two lists
Following are quick examples of zipping two lists.
# Quick examples of zipping two lists
# Create two lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip()
print(list(zip(marks, countries)))
# Using zip() with for loop
for i,j in zip(marks, countries):
print(i,j)
# Using zip() with enumerate() and for loop
for i,(j,k) in enumerate(zip(marks, countries)):
print(i,j,k)
# Using zip_longest()
from itertools import zip_longest
print(list(zip_longest(marks, countries)))
2. Using zip() to Join Two Lists
The zip() is a built-in function in python that is used to join two or more lists. It takes multiple iterable objects like a list as an argument and joins element by element into a tuple and finally returns a zip object, now convert this to a list to get a list of tuples.
Following is the syntax of the zip() function
# Syntax
list(zip(list1, list2))
Here, list1
is the first input list and list2
is the second input list.
Let’s have two lists such that the first list stores integers and the second list stores strings. Zip these two lists using the zip() function.
# Initialize two list
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip()
print(list(zip(marks, countries)))
# Output:
# [(90, 'China'), (56, 'Japan'), (78, 'Russia'), (86, 'India')]
3. Using zip() with for loop
We can also zip two lists and use it with the for loop to iterate each zipped element. In this scenario, we need two iterators such that the first iterator is used to iterate the first list and the second iterator is used to iterate the second list.
# Initialize lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip() with for loop
for i,j in zip(marks, countries):
print(i,j)
# Output:
# 90 China
# 56 Japan
# 78 Russia
# 86 India
4. Using zip() with for loop & enumerate()
This scenario is similar to the above one which will use for loop with enumerate(). The thing is that we need to iterate the index along with other iterators. The iterators that will iterate the lists are placed in a tuple.
# Initialize lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip() with enumerate() and for loop
for i,(j,k) in enumerate(zip(marks, countries)):
print(j,k)
# Output:
# 90 China
# 56 Japan
# 78 Russia
# 86 India
5. Using zip_longest()
The zip_longest() is similar to zip() method. It is available in itertools package. So we need to import itertools module to use it.
5.1 Syntax
Following is the syntax of the zip_longest()
# Syntax
list(itertools.zip_longest(list1, list2))
Here, list1 is the first input list and list2 is the second input list.
5.2 Example
Let’s have two lists such that the first list stores integers and the second list stores strings. Zip these two lists using the itertools.zip_longest() function.
# Import
from itertools import zip_longest
# Initialize two lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip_longest()
print(list(zip_longest(marks, countries)))
# Output:
# [(90, 'China'), (56, 'Japan'), (78, 'Russia'), (86, 'India')]
5.3 Using zip_longest() with for loop
Similarly, We can zip two lists and use for loop to iterate the zipped values.
# Import
from itertools import zip_longest
# Initialize lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip_longest() with for loop
for i,j in zip_longest(marks, countries):
print(i,j)
# Output:
# 90 China
# 56 Japan
# 78 Russia
# 86 India
5.4 Using zip_longest() with for loop & enumerate()
This scenario is similar to the above one which will use for loop with enumerate(). The thing is that we need to iterate the index along with other iterators. The iterators that will iterate the lists are placed in a tuple.
# Import
from itertools import zip_longest
# Initialize lists
marks=[90,56,78,86]
countries=['China','Japan','Russia','India']
# Using zip_longest() with enumerate() and for loop
for i,(j,k) in enumerate(zip_longest(marks, countries)):
print(j,k)
# Output:
# 90 China
# 56 Japan
# 78 Russia
# 86 India
6. Conclusion
We have seen how to zip two lists in python using zip() and itertools.zip_longest() methods. We also used these methods with for loop and enumerate().
Related Articles
- Add 1 to each Element in the List in Python
- Python Doubly Linked List with Examples
- How to Prepend to a List
- Python Print List without Brackets
- Get Index of Max of List in Python
- Python Split a list into evenly sized chunks?