Access the Index in Python 'for' Loops

Kontext Kontext visibility 58 event 2022-07-15 access_time 2 years ago language English

Python for loop is commonly used to iterate through a list. Sometimes it might be useful to also utilize the index in the for loops. We can achieve this by using built-in enumerate function or via an variable. 

Code snippets

Via enumerate function

numbers = [10,20,30]
for i, number in enumerate(numbers):



Via an variable

numbers = [10,20,30]
i = 0
for number in numbers:
    i = i + 1

Results are the same as the above one.

More from Kontext
info Last modified by Kontext 2 years ago copyright This page is subject to Site terms.
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts