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

Kurssisisältö

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

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.

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:

python
  • 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.

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

12345
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
copy
Tehtävä

Swipe to start coding

As a traveler, you have a list of potential destinations and want to assign a unique identifier to each one based on its position in the list.

  • Use the enumerate() function to iterate through the countries list.
  • Assign an ID number to each destination.
  • Store the results in a list where each entry follows the format: "<Country>-id#<Index>".
py

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6
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.

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:

python
  • 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.

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

12345
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
copy
Tehtävä

Swipe to start coding

As a traveler, you have a list of potential destinations and want to assign a unique identifier to each one based on its position in the list.

  • Use the enumerate() function to iterate through the countries list.
  • Assign an ID number to each destination.
  • Store the results in a list where each entry follows the format: "<Country>-id#<Index>".
py

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt