In this tutorial, our goal is to teach you all about the Python enumerate()
and zip()
functions by stepping through several examples.
The enumerate()
function returns indexes of all items in iterables (lists, dictionaries, sets, etc.) whereas the zip()
function is used to aggregate, or combine, multiple iterables. I know those are a bunch of big words, but our examples will help!
Python Enumerate Function
The enumerate()
function in Python is used to retrieve indexes of all the items in an iterable. An iterable is a collection of items in Python such as lists, dictionaries and sets.
Though you can use the index()
function to get the index of an item in an iterable, the index()
function only returns the index of the first occurrence of the item.
For instance, in the
nums = [10,15,20,25,30,14,20,19,34,14,20,12]
index_20 = nums.index(20)
print(index_20)
Output:
2
With the enumerate()
function, you can get indexes of all the items even if they occur multiple times in a list. The enumerate()
function returns an object of “enumerate” type as shown in the output of the following script.
type_enum = type(enumerate(nums))
print(type_enum)
Output:
<class 'enumerate'>
The enumerate()
function returns tuples of two items where the first item corresponds to the index while the second tuple item corresponds to the actual item in the corresponding iterable.
You can convert the object returned by the enumerate()
function into a list of tuples containing indexes and corresponding items as shown below:
print(list(enumerate(nums)))
Output:
Now you can see all the indexes and their corresponding items from the
[(0, 10), (1, 15), (2, 20), (3, 25), (4, 30), (5, 14), (6, 20), (7, 19), (8, 34), (9, 14), (10, 20), (11, 12)]
You can use a for loop to iterate through the tuples returned by the enumerate()
function as shown below where the first tuple item is accessed via index 0, while the second tuple item is accessed via index 1.
for i in enumerate(nums):
print("Index =>",i[0]," - value =>", i[1])
Output:
Index => 0 - value => 10
Index => 1 - value => 15
Index => 2 - value => 20
Index => 3 - value => 25
Index => 4 - value => 30
Index => 5 - value => 14
Index => 6 - value => 20
Index => 7 - value => 19
Index => 8 - value => 34
Index => 9 - value => 14
Index => 10 - value => 20
Index => 11 - value => 12
You can also use the tuple unpacking to directly access the indexes and items in tuples returned by the enumerate()
function. Here’s an example:
for i, j in enumerate(nums):
print("Index =>",i," - value =>", j)
Output:
Index => 0 - value => 10
Index => 1 - value => 15
Index => 2 - value => 20
Index => 3 - value => 25
Index => 4 - value => 30
Index => 5 - value => 14
Index => 6 - value => 20
Index => 7 - value => 19
Index => 8 - value => 34
Index => 9 - value => 14
Index => 10 - value => 20
Index => 11 - value => 12
As an another example, the following script shows how you can use the enumerate()
function to get all the indexes for the integer 20 in the “nums” list.
for i in enumerate(nums):
if i[1] == 20:
print("Value =>",i[1]," - index =>", i[0])
Output:
Value => 20 - index => 2
Value => 20 - index => 6
Value => 20 - index => 10
You can also reset the starting index for the tuples returned by the enumerate()
by passing a value to the start
parameter. For instance, the script below sets 8 as the starting index value for the list of tuples returned by the enumerate()
function.
for i in enumerate(nums, start = 8):
print("Index =>",i[0]," - value =>", i[1])
Output:
Index => 8 - value => 10
Index => 9 - value => 15
Index => 10 - value => 20
Index => 11 - value => 25
Index => 12 - value => 30
Index => 13 - value => 14
Index => 14 - value => 20
Index => 15 - value => 19
Index => 16 - value => 34
Index => 17 - value => 14
Index => 18 - value => 20
Index => 19 - value => 12
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Python Zip Function
The zip()
function in Python is used to aggregate multiple iterables. The zip()
function returns a list of tuples where each tuple contains the items from the matching indexes of the original iterables. This is best explained with an example.
The script below contains three lists: zip()
function. If you print the list of tuples returned by the zip()
function you’ll see that each tuple item contains items from the matching indexes in the original lists.
For instance, the first tuple contains three items: ‘john’, 20, ‘male’. These three items occur at 0th indexes of the
names = ["john", "nick", "sally", "Elizabeth", "Mike"]
ages = [20, 25, 35, 22, 29]
genders = ["male", "male", "female", "female", "male"]
records = list(zip(names, ages, genders))
print(records)
Output:
[('john', 20, 'male'), ('nick', 25, 'male'), ('sally', 35, 'female'), ('Elizabeth', 22, 'female'), ('Mike', 29, 'male')]
Just as you saw with the enumerate()
function, you can use tuple unpacking to get the individual items from the tuples returned by the zip()
function. Here’s an example:
for name, age, gender in zip(names, ages, genders):
print("Name=>", name, " -Age=>", age, " -Gender=>", gender)
Output:
Name=> john -Age=> 20 -Gender=> male
Name=> nick -Age=> 25 -Gender=> male
Name=> sally -Age=> 35 -Gender=> female
Name=> Elizabeth -Age=> 22 -Gender=> female
Name=> Mike -Age=> 29 -Gender=> male
In some cases, the iterables being aggregated, or combined, using the zip()
function contain an unequal number of items. In such cases, only those items from multiple iterables are aggregated that correspond to the items in the smallest iterable.
For instance, in the script below the zip()
function to aggregate the
names = ["john", "nick", "sally", "Elizabeth", "Mike"]
ages = [20, 25, 35, 22, 29]
genders = ["male", "male"]
records = list(zip(names, ages, genders))
print(records)
Output:
[('john', 20, 'male'), ('nick', 25, 'male')]
Combining Enumerate with Zip
You can also call the enumerate()
function on the output returned by the zip()
function. In such cases, the enumerate()
function will return tuples where the items at the first index in the tuple will correspond to the indexes of the tuples returned by the zip()
function and the items at the second index will correspond to the actual tuple returned by the zip()
function. This sounds a little complicated so follow this example to see how you can use the enumerate()
and zip()
functions together.
names = ["john", "nick", "sally", "Elizabeth", "Mike"]
ages = [20, 25, 35, 22, 29]
genders = ["male", "male", "female", "female", "male"]
for i, record in enumerate(zip(names, ages, genders)):
print("Index=>", i," -Name=>", record[0], " -Age=>", record[1], " -Gender=>", record[2])
Output:
Index=> 0 -Name=> john -Age=> 20 -Gender=> male
Index=> 1 -Name=> nick -Age=> 25 -Gender=> male
Index=> 2 -Name=> sally -Age=> 35 -Gender=> female
Index=> 3 -Name=> Elizabeth -Age=> 22 -Gender=> female
Index=> 4 -Name=> Mike -Age=> 29 -Gender=> male
Notice how we now have a table of the lists with an associated index. If you found this tutorial helpful, be sure to subscribe using the form below. My goal is to make sure you’re getting the most out of Python so you can intelligently tackle your next programming project.
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.