Built in function Grammar/Environments

[Python] How to Use zip() Function

This article shows how to use zip() function.

Contents

  • What is zip function?
  • Basic usage of zip function
  • How to zip 3 or more iterables
  • Case with different number of iterable elements
  • Convert 1D list to 2D list using zip() function and comprehension
  • Create dictionary from two lists
  • Combination of zip() and enumerate() functions

sponsored link

What is zip function?

In the beginning, I explain briefly zip( ) function.

The zip() function is built-in function in Python.

So, We can use zip( ) function without "import".

The zip( ) function creates an iterator that will aggregate elements from multiple iterables.

The zip( ) function is often used with the "for loop", for example, to simultaneously get elements from multiple lists.

Basic usage of zip function

I show basic usage of zip( ) function.

The zip( ) function takes iterables (such as list, tuple, etc.) as the arguments.

How to write

zip object = zip( iterator1, iterator2, iterator3 ... )

Here is an example code.

fruits = ['Apple', 'Banana', 'Orange', 'Grape', 'Lemon']
numbers = [1, 2, 3, 4, 5]

zipped = zip(fruits, numbers)
print(zipped)
# <zip object at 0x000001B37853ACC8>

print(list(zipped))
# [('Apple', 1), ('Banana', 2), ('Orange', 3), ('Grape', 4), ('Lemon', 5)]

> print(zipped)
> # <zip object at 0x000001B37853ACC8>

A zip object is generated.

> print(list(zip_test))

Since the elements cannot be checked as a zip object, they are converted to list type with the list( ) function.

Use the zip( ) function with "for loop"

The zip( ) function is often used with "for loop".

By using the zip( ) function, elements used within "for loop" can be got from multiple lists simultaneously.

Here is an example of "for loop".

fruits = ['Apple', 'Banana', 'Orange', 'Grape', 'Lemon']
numbers = [1, 2, 3, 4, 5]

for fruit, number in zip(fruits, numbers) :
    print(fruit, number)
# Apple 1
# Banana 2
# Orange 3
# Grape 4
# Lemon 5

sponsored link

How to zip 3 or more iterables

The zip( ) function can also be used with three or more iterables.

Here is an example code.

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D', 'E']
lowercase = ['a', 'b', 'c', 'd', 'e']

for n, u, l in zip(numbers, uppercase, lowercase) :
    print(n, u, l)
# 1 A a
# 2 B b
# 3 C c
# 4 D d
# 5 E e

Case with different number of iterable elements

If the number of elements is different, processing is performed according to the smaller number of elements.

Here is an example code.

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D']
lowercase = ['a', 'b', 'c']

for n, u, l in zip(numbers, uppercase, lowercase) :
    print(n, u, l)
# 1 A a
# 2 B b
# 3 C c

Process according to the larger number of elements

If you want to process according to the larger number of elements, use the "zip_longest()" function in the "itertools" module.

The "itertools" is a standard python library.

Here is an example code.

from itertools import zip_longest

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D']
lowercase = ['a', 'b', 'c']

for n, u, l in zip_longest(numbers, uppercase, lowercase) :
    print(n, u, l)
# 1 A a
# 2 B b
# 3 C c
# 4 D None
# 5 None None

If there are not enough elements, "None" is returned.

If you want to enter an arbitrary value for a missing element, specify the "fillvalue" as argument.

from itertools import zip_longest

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D']
lowercase = ['a', 'b', 'c']

for n, u, l in zip_longest(numbers, uppercase, lowercase, fillvalue='---') :
    print(n, u, l)
# 1 A a
# 2 B b
# 3 C c
# 4 D ---
# 5 --- ---

sponsored link

Convert 1D list to 2D list using zip() function and comprehension

By using zip( ) function, 1D list can be converted to 2D list.

Here is an example code.

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D', 'E']
lowercase = ['a', 'b', 'c', 'd', 'e']

list_2d = list(zip(numbers, uppercase, lowercase))
print(list_2d)
# [(1, 'A', 'a'), (2, 'B', 'b'), (3, 'C', 'c'), (4, 'D', 'd'), (5, 'E', 'e')]

In this example, the elements of the list are tuples.

If you want to convert to 2D list, use comprehension.

numbers = [1, 2, 3, 4, 5]
uppercase = ['A', 'B', 'C', 'D', 'E']
lowercase = ['a', 'b', 'c', 'd', 'e']
list_2d = [ [n, u, l] for n, u, l in zip(numbers, uppercase, lowercase)]
print(list_2d)
# [[1, 'A', 'a'], [2, 'B', 'b'], [3, 'C', 'c'], [4, 'D', 'd'], [5, 'E', 'e']]

How to use of comprehension is explained in this article.

How to Use Comprehension

Create dictionary from two lists

By using the zip( ) function, a dictionary can be created from two lists.

The zip( ) function merges the lists of keys and values, and the dict( ) function converts the "zip object" to dictionary type.

Here is an example code.

numbers = [1, 2, 3, 4, 5]
fruits = ['Apple', 'Banana', 'Orange', 'Grape', 'Lemon']

fruits_dict = dict(zip(numbers, fruits))
print(type(fruits_dict))
# <class 'dict'>
print(fruits_dict)
# {1: 'Apple', 2: 'Banana', 3: 'Orange', 4: 'Grape', 5: 'Lemon'}

print(fruits_dict[2])
# Banana

sponsored link

Combination of zip() and enumerate() functions

The enumerate( ) function allows simultaneous getting the elements and indices of an iterable object.

Here is an example code of the enumerate( ) function.

fruits = ['Apple', 'Banana', 'Orange', 'Grape', 'Lemon']

for i, fruit in enumerate(fruits) :
    print(i, fruit)
# 0 Apple
# 1 Banana
# 2 Orange
# 3 Grape
# 4 Lemon

When using the enumerate() and zip() functions at the same time, the variable must be received as a tuple.

uppercase = ['A', 'B', 'C', 'D', 'E']
lowercase = ['a', 'b', 'c', 'd', 'e']

for i, (u, l) in enumerate(zip(uppercase, lowercase)) :
    print(i, u, l)
# 0 A a
# 1 B b
# 2 C c
# 3 D d
# 4 E e

Another way of writing

uppercase = ['A', 'B', 'C', 'D', 'E']
lowercase = ['a', 'b', 'c', 'd', 'e']

for i, value_tuple in enumerate(zip(uppercase, lowercase)) :
    print(i, value_tuple)
# 0 ('A', 'a')
# 1 ('B', 'b')
# 2 ('C', 'c')
# 3 ('D', 'd')
# 4 ('E', 'e')

How to use of the enumerate() function is explained in this article.

How to Use enumerate() Function

sponsored link

-Built in function, Grammar/Environments
-,