Use enumerate() and zip() together in Python
Last Updated :
25 Apr, 2025
In Python, zip() combines multiple iterables into tuples, pairing elements at the same index, while enumerate() adds a counter to an iterable, allowing us to track the index. By combining both, we can iterate over multiple sequences simultaneously while keeping track of the index, which is useful when we need both the elements and their positions. For Example:
Python
a = [10, 20, 30]
b = ['a', 'b', 'c']
for idx, (num, char) in enumerate(zip(a,b), start=1):
print(idx, num, char)
Output1 10 a
2 20 b
3 30 c
Explanation: Loop prints the index (starting from 1) along with the corresponding elements from lists a and b. zip() pairs the elements and enumerate() tracks the index starting from 1.
Syntax of enumerate() and zip() together
for var1,var2,.,var n in enumerate(zip(list1,list2,..,list n))
Parameters:
- zip(list1, list2, ..., list_n) combines multiple iterables element-wise into tuples.
- enumerate(iterable, start=0) adds an index (starting from start, default is 0) to the tuples.
- var1, var2, ..., var_n variables to unpack the indexed tuple.
Examples
Example 1: Iterating over multiple lists
Python
a = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
b = ['java', 'python', 'R', 'cpp', 'bigdata']
c = [78, 100, 97, 89, 80]
for i, (name, subject, mark) in enumerate(zip(a, b, c)):
print(i, name, subject, mark)
Output0 sravan java 78
1 bobby python 100
2 ojaswi R 97
3 rohith cpp 89
4 gnanesh bigdata 80
Explanation: zip() pairs elements from a, b and c into tuples, while enumerate() adds an index. The loop extracts and prints i, name, subject and mark.
Example 2: Storing the tuple output
Python
a = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
b = ['java', 'python', 'R', 'cpp', 'bigdata']
c = [78, 100, 97, 89, 80]
for i, t in enumerate(zip(a,b,c)):
print(i, t)
Output0 ('sravan', 'java', 78)
1 ('bobby', 'python', 100)
2 ('ojaswi', 'R', 97)
3 ('rohith', 'cpp', 89)
4 ('gnanesh', 'bigdata', 80)
Explanation: zip() pairs elements from a, b and c into tuples, while enumerate() adds an index. The loop extracts and prints i and the tuple t containing name, subject and mark.
Example 3: Accessing Tuple Elements Using Indexing
Python
a = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
b = ['java', 'python', 'R', 'cpp', 'bigdata']
c = [78, 100, 97, 89, 80]
for i, t in enumerate(zip(a,b,c)):
print(i, t[0], t[1], t[2])
Output0 sravan java 78
1 bobby python 100
2 ojaswi R 97
3 rohith cpp 89
4 gnanesh bigdata 80
Explanation: zip() pairs elements from a, b and c into tuples, while enumerate() adds an index. The loop extracts and prints i, t[0] (name), t[1] (subject) and t[2] (mark) from each tuple.
Related Articles:
Similar Reads
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
How to iterate through list of tuples in Python In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these
2 min read
Working with zip files in Python This article explains how one can perform various operations on a zip file using a simple python program. What is a zip file? ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectl
5 min read
How to Concatenate Two Lists Index Wise in Python Concatenating two lists index-wise means combining corresponding elements from both lists into a single element, typically a string, at each index . For example, given two lists like a = ["gf", "i", "be"] and b = ["g", "s", "st"], the task is to concatenate each element from a with the corresponding
3 min read
Ways to Iterate Tuple List of Lists - Python In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi
3 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Looping Techniques in Python Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects that require a specific technique with loops maintaining the overall structure of code. Â A lot of
6 min read
Create Password Protected Zip of a file using Python ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a single file containing one or more compressed files, offering an
2 min read
zip() in Python The zip() function in Python combines multiple iterables such as lists, tuples, strings, dict etc, into a single iterator of tuples. Each tuple contains elements from the input iterables that are at the same position.Letâs consider an example where we need to pair student names with their test score
3 min read