Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The enumerate() Function | The for Loop
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

book
The enumerate() Function

The enumerate() function is incredibly useful when you need to access both the value and its index in a sequence, such as a list or a string. This allows you to work with items while keeping track of their position in the sequence.

How enumerate() Works with Lists

In Python, lists are ordered data structures, meaning each item has a unique index. The enumerate() function makes it easy to retrieve both the index and the value simultaneously.

The syntax for using enumerate() is:

  • index: refers to the position of an element in the list. Python uses 0-based indexing, meaning the first element has an index of 0;
  • value: refers to the actual element at a given index.

Example: Printing City Names with Their Indexes

Let's apply enumerate() to our travel_list to print each city along with its index:

123456
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(f"{index} - {city}")
copy
Task
test

Swipe to show code editor

Write a program that:

  1. Uses enumerate() to identify cities at even-indexed positions in the travel_list.
  2. Creates a new list containing only these cities.
  3. Prints the new list of even-indexed cities.

Note

The append() method adds an item to the end of a list. For example, my_list.append("item").

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8
toggle bottom row

book
The enumerate() Function

The enumerate() function is incredibly useful when you need to access both the value and its index in a sequence, such as a list or a string. This allows you to work with items while keeping track of their position in the sequence.

How enumerate() Works with Lists

In Python, lists are ordered data structures, meaning each item has a unique index. The enumerate() function makes it easy to retrieve both the index and the value simultaneously.

The syntax for using enumerate() is:

  • index: refers to the position of an element in the list. Python uses 0-based indexing, meaning the first element has an index of 0;
  • value: refers to the actual element at a given index.

Example: Printing City Names with Their Indexes

Let's apply enumerate() to our travel_list to print each city along with its index:

123456
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(f"{index} - {city}")
copy
Task
test

Swipe to show code editor

Write a program that:

  1. Uses enumerate() to identify cities at even-indexed positions in the travel_list.
  2. Creates a new list containing only these cities.
  3. Prints the new list of even-indexed cities.

Note

The append() method adds an item to the end of a list. For example, my_list.append("item").

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt