Built with Tutify.io

    AIStudyLabs

    Python zip() Two Lists with Examples

    Python zip() Two Lists with Examples

    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.

    <a id="h-1-quick-examples-of-zipping-two-lists"></a>

    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)))
    

    <a id="h-2-using-zip-to-join-two-lists"></a>

    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')]
    

    <a id="h-3-using-zip-with-for-loop"></a>

    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
    

    <a id="h-4-using-zip-with-for-loop-amp-enumerate"></a>

    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
    

    <a id="h-5-using-zip-longest"></a>

    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.

    <a id="h-5-1-syntax"></a>

    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.

    <a id="h-5-2-example"></a>

    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')]
    

    <a id="h-5-3-using-zip-longest-with-for-loop"></a>

    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
    

    <a id="h-5-4-using-zip-longest-with-for-loop-amp-enumerate"></a>

    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
    

    <a id="h-6-conclusion"></a>

    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().

    <a id="h-related-articles"></a>

    Related Articles