Created
June 3, 2023 17:03
-
-
Save eder-projetos-dev/610e9e52702af4065c1fa944c006a389 to your computer and use it in GitHub Desktop.
Python - Enumerate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fruits = ['apple', 'banana', 'orange'] | |
for index, fruit in enumerate(fruits): | |
print(f"Index: {index}, Fruit: {fruit}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enumerate
I think enumerate() is a handy function when I need to iterate over a list and also access the index of each element. It eliminates the need for maintaining a separate counter variable and improves code readability. For instance, I can use enumerate() to iterate over a list of fruits and print both the index and the fruit name.
https://levelup.gitconnected.com/11-tricks-that-will-make-your-life-easier-as-a-python-developer-da29e4306675