Open In App

Use enumerate() and zip() together in Python

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




# create a list of names
names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
  
# create a list of subjects
subjects = ['java', 'python', 'R', 'cpp', 'bigdata']
  
# create a list of marks
marks = [78, 100, 97, 89, 80]
  
# use enumerate() and zip() function
# to iterate the lists
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




# create a list of names
names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
  
# create a list of subjects
subjects = ['java', 'python', 'R', 'cpp', 'bigdata']
  
# create a list of marks
marks = [78, 100, 97, 89, 80]
  
# use enumerate() and zip() function
# to iterate the lists with t function
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




# create a list of names
names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
  
# create a list of subjects
subjects = ['java', 'python', 'R', 'cpp', 'bigdata']
  
# create a list of marks
marks = [78, 100, 97, 89, 80]
  
# use enumerate() and zip() function
# to iterate the lists with t function
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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads