Get Started

Built-in practice: enumerate()

In this exercise, you'll practice using Python's built-in function enumerate(). This function is useful for obtaining an indexed list. For example, suppose you had a list of people that arrived at a party you are hosting. The list is ordered by arrival (Jerry was the first to arrive, followed by Kramer, etc.):

names = ['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']

If you wanted to attach an index representing a person's arrival order, you could use the following for loop:

indexed_names = []
for i in range(len(names)):
    index_name = (i, names[i])
    indexed_names.append(index_name)

[(0,'Jerry'),(1,'Kramer'),(2,'Elaine'),(3,'George'),(4,'Newman')]

But, that's not the most efficient solution. Let's explore how to use enumerate() to make this more efficient.

This is a part of the course

“Writing Efficient Python Code”

View Course

Exercise instructions

  • Instead of using for i in range(len(names)), update the for loop to use i as the index variable and name as the iterator variable and use enumerate().
  • Rewrite the previous for loop using enumerate() and list comprehension to create a new list, indexed_names_comp.
  • Create another list (indexed_names_unpack) by using the star character (*) to unpack the enumerate object created from using enumerate() on names. This time, start the index for enumerate() at one instead of zero.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Rewrite the for loop to use enumerate
indexed_names = []
for ____,_____ in ____(names):
    index_name = (i,name)
    indexed_names.append(index_name) 
print(indexed_names)

# Rewrite the above for loop using list comprehension
indexed_names_comp = [(____,____) for i,name in ____(names)]
print(indexed_names_comp)

# Unpack an enumerate object with a starting index of one
indexed_names_unpack = [____(names, ____)]
print(indexed_names_unpack)

This exercise is part of the course

Writing Efficient Python Code

IntermediateSkill Level
4.7+
53 reviews

Learn to write efficient code that executes quickly and allocates resources skillfully to avoid unnecessary overhead.

In this chapter, you'll learn what it means to write efficient Python code. You'll explore Python's Standard Library, learn about NumPy arrays, and practice using some of Python's built-in tools. This chapter builds a foundation for the concepts covered ahead.

Exercise 1: Welcome!Exercise 2: Pop quiz: what is efficientExercise 3: A taste of things to comeExercise 4: Zen of PythonExercise 5: Building with built-insExercise 6: Built-in practice: range()Exercise 7: Built-in practice: enumerate()
Exercise 8: Built-in practice: map()Exercise 9: The power of NumPy arraysExercise 10: Practice with NumPy arraysExercise 11: Bringing it all together: Festivus!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free