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