Iterate over Index Numbers and Elements in a List Using Enumerate

Import Library

from collections import defaultdict

Definition of Enumerate

enumerate() is a powerful Python built-in function that helps you easily iterate over indexes and values in a sequence, iterator or other object that supports iteration.

enumerate() returns an enumerate object.

enumerate([1, 2, 3])
<enumerate at 0x10e3ced38>

Simple Example

You ask Jen to make a list of dinner choices and rank them; you want to see the values and numeric ranks.

jen_dinner_choices = ["pasta", "pizza", "salad", "burger"]

There's two ways to preview the values and ranks.

Loop and print

for index, choice in enumerate(jen_dinner_choices):
    print(index, choice)
0 pasta
1 pizza
2 salad
3 burger

Create list of tuples that contain (index, value)

list(enumerate(jen_dinner_choices))
[(0, 'pasta'), (1, 'pizza'), (2, 'salad'), (3, 'burger')]

Example Continued

You ask preferences of three more friends to rank these four choices and want to see which is the top pick.

ed_dinner_choices = ["burger", "pizza", "pasta", "salad"]
matt_dinner_choices = ["salad", "pasta", "burger", "pizza"]
pam_dinner_choices = ["pizza", "pasta", "burger", "salad"]

Let's sum up the ranks of food options. The lowest sum food option is the dinner pick!

We'll use a Python built-in dictionary-like object called defaultdict.

# define empty defaultdict to store keys as food options (strings) and values as cumulative rankings (ints)
# default value of int in line below is 0
choice_ranks = defaultdict(int)

def increment_choice_ranks(person_choices_ranks, food_option_rank):
    """Add individual person's food rankings to cumulative rankings defaultdict

    Keyword arguments: 
    person_choices_ranks -- person's ranking of four food options (list)
    food_option_rank -- cumulative food rankings (defaultdict)
    """
    # loop through index and values of person's food rankings
    for index, choice in enumerate(person_choices_ranks):

        print("food rank is: {0}".format(index))
        print("food choice is: {0}".format(choice))

        # increment cumulative ranking value by person's rank
        food_option_rank[choice] += index

        print("new food_option_rank is: {0}".format(food_option_rank))
        print(" ")

    # return our cumulative food rankings
    return food_option_rank

Call our increment_choice_ranks function using each person's food choices; each time, we increment our choice_ranks defaultdict.

The output is printed below to show how the defaultdict changes with each new food ranking.

choice_ranks = increment_choice_ranks(jen_dinner_choices, choice_ranks)
choice_ranks = increment_choice_ranks(ed_dinner_choices, choice_ranks)
choice_ranks = increment_choice_ranks(matt_dinner_choices, choice_ranks)
choice_ranks = increment_choice_ranks(pam_dinner_choices, choice_ranks)
food rank is: 0
food choice is: pasta
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0})

food rank is: 1
food choice is: pizza
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0, 'pizza': 1})

food rank is: 2
food choice is: salad
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0, 'pizza': 1, 'salad': 2})

food rank is: 3
food choice is: burger
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0, 'pizza': 1, 'salad': 2, 'burger': 3})

food rank is: 0
food choice is: burger
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0, 'pizza': 1, 'salad': 2, 'burger': 3})

food rank is: 1
food choice is: pizza
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 0, 'pizza': 2, 'salad': 2, 'burger': 3})

food rank is: 2
food choice is: pasta
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 2, 'pizza': 2, 'salad': 2, 'burger': 3})

food rank is: 3
food choice is: salad
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 2, 'pizza': 2, 'salad': 5, 'burger': 3})

food rank is: 0
food choice is: salad
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 2, 'pizza': 2, 'salad': 5, 'burger': 3})

food rank is: 1
food choice is: pasta
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 3, 'pizza': 2, 'salad': 5, 'burger': 3})

food rank is: 2
food choice is: burger
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 3, 'pizza': 2, 'salad': 5, 'burger': 5})

food rank is: 3
food choice is: pizza
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 3, 'pizza': 5, 'salad': 5, 'burger': 5})

food rank is: 0
food choice is: pizza
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 3, 'pizza': 5, 'salad': 5, 'burger': 5})

food rank is: 1
food choice is: pasta
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 4, 'pizza': 5, 'salad': 5, 'burger': 5})

food rank is: 2
food choice is: burger
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 4, 'pizza': 5, 'salad': 5, 'burger': 7})

food rank is: 3
food choice is: salad
new food_option_rank is: defaultdict(<class 'int'>, {'pasta': 4, 'pizza': 5, 'salad': 8, 'burger': 7})
choice_ranks
defaultdict(int, {'burger': 7, 'pasta': 4, 'pizza': 5, 'salad': 8})

Pasta is the winner!!