What is enumerate and zip in python?

In Python, enumerate() and zip() are useful when iterating elements of iterable (list, tuple, etc.) in a for loop.

  • for loop in Python (with range, enumerate, zip, etc.)

You can get the index with enumerate(), and get the elements of multiple iterables with zip().

  • How to start enumerate() at 1 in Python
  • zip() in Python: Get elements from multiple lists

This article describes the notes when using enumerate() and zip() together.

Notes on using enumerate() and zip() together

If you want to get the elements of multiple lists and indexes, you can use enumerate() and zip() together.

In this case, you need to enclose the elements of zip() in parentheses, like for i, (a, b, ...) in enumerate(zip( ... )).

names = ['Alice', 'Bob', 'Charlie']
ages = [24, 50, 18]

for i, (name, age) in enumerate(zip(names, ages)):
    print(i, name, age)
# 0 Alice 24
# 1 Bob 50
# 2 Charlie 18

You can also receive the elements of zip() as a tuple.

for i, t in enumerate(zip(names, ages)):
    print(i, t)
# 0 ('Alice', 24)
# 1 ('Bob', 50)
# 2 ('Charlie', 18)

for i, t in enumerate(zip(names, ages)):
    print(i, t[0], t[1])
# 0 Alice 24
# 1 Bob 50
# 2 Charlie 18

Note that the functions count() and zip() of the itertools module of the standard library can be used to create a non-nested form like (i, a, b).

  • Infinite iterators in Python (itertools.count, cycle, repeat)

In this article, we will discuss how to use enumerate() and zip() functions in python.

Python enumerate() is used to convert into a list of tuples using the list() method.

Syntax:

enumerate(iterable, start=0)

Parameters:

  • Iterable: any object that supports iteration
  • Start: the index value from which the counter is  to be started, by default it is 0

Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. 

Syntax:

zip(*iterators) 

Using Both, we can iterate two/more lists/objects by using enumerate and zip functions at a time.

Syntax:

enumerate(zip(list1,list2,.,list n))

We can iterate this in for loop.

Syntax:

for var1,var2,.,var n in enumerate(zip(list1,list2,..,list n))

where,

  • list1,list2 ,. are the input lists
  • var1 , var2,… are the iterators to iterate the lists

Example: Using enumerate() and zip() together in Python

Python3

names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']

subjects = ['java', 'python', 'R', 'cpp', 'bigdata']

marks = [78, 100, 97, 89, 80]

for i, (names, subjects, marks) in enumerate(zip(names, subjects, marks)):

    print(i, names, subjects, marks)

Output:

0 sravan java 78
1 bobby python 100
2 ojaswi R 97
3 rohith cpp 89
4 gnanesh bigdata 80

We can also do this by using tuple(t)

Syntax:

for i, t in enumerate(zip(names, subjects,marks))

Its returns the data in the tuple format

Example: Using enumerate() and zip() together in Python

Python3

names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']

subjects = ['java', 'python', 'R', 'cpp', 'bigdata']

marks = [78, 100, 97, 89, 80]

for i, t in enumerate(zip(names, subjects, marks)):

    print(i, t)

Output:

0 ('sravan', 'java', 78)
1 ('bobby', 'python', 100)
2 ('ojaswi', 'R', 97)
3 ('rohith', 'cpp', 89)
4 ('gnanesh', 'bigdata', 80)

we can also use t[index] in the above approach to get output

Example: Using enumerate() and zip() together in Python

Python3

names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']

subjects = ['java', 'python', 'R', 'cpp', 'bigdata']

marks = [78, 100, 97, 89, 80]

for i, t in enumerate(zip(names, subjects, marks)):

    print(i, t[0], t[1], t[2])

Output:

0 sravan java 78
1 bobby python 100
2 ojaswi R 97
3 rohith cpp 89
4 gnanesh bigdata 80

What is a enumerate in Python?

What does enumerate do in Python? The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.

What is zip () in Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

Can zip function be used with enumerate in Python?

Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. Using Both, we can iterate two/more lists/objects by using enumerate and zip functions at a time.

Can zip function be used with enumerate?

If you want to get the elements of multiple lists and indexes, you can use enumerate() and zip() together. In this case, you need to enclose the elements of zip() in parentheses, like for i, (a, b, ...) in enumerate(zip( ... )) . You can also receive the elements of zip() as a tuple.

Tải thêm tài liệu liên quan đến bài viết What is enumerate and zip in python?