Python Enumerate

ยท

3 min read

The enumerate() function returns a sequence of (index, item) tuples.

Enumerate takes two parameters: iterable and start.

enumerate(iterable, start)

Iterable - the collection of items to be returned as an enumerate object

Start - starting index for the enumerate object. If the start value is omitted the default value is 0

How to Use enumerate() in python

countries = ["France", "ROC", "United States of America", "United States of America", "Italy", "ROC", "Canada", "Spain", "Canada", "Great Britian"]

list(enumerate(countries))

-----------------------------------------------------------------------
[(0, 'France'),
 (1, 'ROC'),
 (2, 'United States of America'),
 (3, 'United States of America'),
 (4, 'Italy'),
 (5, 'ROC'),
 (6, 'Canada'),
 (7, 'Spain'),
 (8, 'Canada'),
 (9, 'Great Britian')]

In our output, we are given a list of tuples that return the index and its corresponding item.

๐Ÿ“ Note: Because we didnโ€™t declare a start value our index is a default of 0.

For Loop

for index, country in enumerate(countries, start=1):
    print(index, country)
-----------------------------------------------------------------------
1 France
2 ROC
3 United States of America
4 United States of America
5 Italy
6 ROC
7 Canada
8 Spain
9 Canada
10 Great Britian

Youโ€™ll most likely encounter the enumerate function using a for loop to iterate through objects.

Our item in this for loop is ranked placed countries.

In real life, we associate gold, silver, and bronze medals with first, second, and third place. So the start parameter has been changed to 1 to reflect those places in our output.

Why use enumerate?

A more complex example is one in which we want to find a country's rankings for a single event given an ordered list. Since multiple people from one country can qualify for an event we can create a hashmap.

#Store the index position of duplicate items
countries = ["France", "ROC", "United States of America", "United States of America",
                   "Italy", "ROC", "Canada", "Spain", "Canada", "Great Britian"]

country_map = {country:[] for country in set(countries)}

print(country_map)

-----------------------------------------------------------------------

{'Italy': [], 'Spain': [], 'France': [], 'Great Britian': [], 'United States of America': [], 'ROC': [], 'Canada': []}

Once a hashmap has been created you can use enumerate in a for loop to store the index for each occurrence of a country's ranked place.

#Use enumerate to store the index for each occurence
for index, country in enumerate(countries, start=1):
    country_map[country].append(index)

country_map

-----------------------------------------------------------------------

{'Italy': [5],
 'Spain': [8],
 'France': [1],
 'Great Britian': [10],
 'United States of America': [3, 4],
 'ROC': [2, 6],
 'Canada': [7, 9]}

Now that we have a dictionary with all our countries and their ranking you can easily find out the rankings of particular countries.

country_map['United States of America']

----------------------------------------------------------------------

[3, 4]

Here we see the United States of America placed in both 3rd and 4th in the Beijing 2022 Figure Skating Ice Dance event.

One way that I remember when I need to use enumerate is when I encounter a problem in which I need to count and assign a number to items in a list in order.

What are some other ways enumerate method can be used? Feel to leave me feedback in the comments. Happy Coding! ๐ŸŒŸ๐Ÿ’ป

source: https://olympics.com/en/olympic-games/beijing-2022/results/figure-skating/ice-dance