Python Enumerate

To start we’re going to create a simple dataframe in python:

If you want to watch a video on our YouTube channel based around the tutorial it’s embedded below.

Example 1 - Enumerate on List

For our first example let’s Enumerate on a list. We will look at a list of sports.
				
					sports = ['ultra running', 'cricket', 'baseball']
				
			
				
					
for index, sport in enumerate(sports):
    print(index, sport)
				
			

0 ultra running
1 cricket
2 baseball

Example 2

We can modify Enumerate to start on 1 instead of 0.
				
					
for index, sport in enumerate(sports, start=1):
    print(index, sport)
				
			

1 ultra running
2 cricket
3 baseball

Example 3 - Enumerate String

While the first two examples look at a list, we can also Enumerate on a String since it’s an iterable.

Each letter will have an index.

				
					word = "DiMaggio"
				
			
				
					
for index, letter in enumerate(word):
    print(index, letter)
				
			

0 D
1 i
2 M
3 a
4 g
5 g
6 i
7 o

Example 4 - Tuples

Now let’s look at another common iterable which is tuples.

				
					teams = ('Rays', 'Yankees', 'Red Sox')
				
			
				
					
for index, team in enumerate(teams):
    print(index, team)
				
			

0 Rays
1 Yankees
2 Red Sox

Example 5 Convert Enumerate to List

You can also convert the final result into a list.

				
					numbers = [17, 27, 99]
				
			
				
					
enumerated_list = list(enumerate(numbers))
print(enumerated_list)
				
			

[(0, 17), (1, 27), (2, 99)]

Example 6 - Dictionary

Another iterable we will take a look at is a dictionary.

				
					my_dict = {'Ohtani': 17, 'Trout': 27, 'Judge': 99}
				
			
				
					
for index, key in enumerate(my_dict):
    print(index, key, my_dict[key])
				
			

0 Ohtani 17
1 Trout 27
2 Judge 99

Example 7 - Enumerate with list comprehension

By using list comprehension you can create a new list in one line of code. We can also use enumerate inside of it.
				
					numbers = [17, 27, 99]
				
			
				
					indexed_numbers = [(index, number) for index, number in enumerate(numbers)]
print(indexed_numbers)
				
			

[(0, 17), (1, 27), (2, 99)]

Example 8 - Enumerate with Logic

Often when working with enumerate, you’ll deal with some sort of logic. Let’s take out hr_hitters list and only print out the hitters where there is an even index.
				
					hr_hitters = ['Bonds', 'Aaron', 'Ruth', 'Pujols', 'Rodriguez']
				
			
				
					
for index, hitter in enumerate(hr_hitters):
    if index % 2 == 0:
        print(f"Even index {index}: {hitter}")
				
			

Even index 0: Bonds
Even index 2:Ruth
Even index 4: Rodriguez

Example 9 - Enumerate with ZIP

Zip allows us to combine multiple iterables. Let’s look at an example which uses both enumerate and Zip. 
				
					names = ['Ohtani', 'Trout', 'Judge']
				
			
				
					numbers = [17, 27, 99]
				
			
				
					
for index, (names, numbers) in enumerate(zip(names, numbers)):
    print(f"{index}: {names} wears number: {numbers}")
				
			

0: Ohtani wears number: 17
1: Trout wears number: 27
2: Judge wears number: 99

Example 10

if you want to update values within a list, you can also use Enumerate. This helps a ton as without this you would be required to create a new lisit.
				
					hrs = [15, 21, 33]
				
			
				
					
for index, value in enumerate(hrs):
    hrs[index] = value * 2
				
			
				
					print(hrs)
				
			

[30, 42, 66]

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *