How to Use enumerate() in Python @DataDaft
How to Use enumerate() in Python  @DataDaft
Uploaded April 2021 | Updated November 2026, 2 days ago
Code available in comment below! This video shows the basics of how to use enumerate() in Python. enumerate() is useful for looping through data structures like lists while having access to each index and the corresponding value for each iteration of the loop.


characters = ["Krillin","Goku", "Vegeta", "Gohan", "Piccolo"]

# enumerate() returns a sequence of (index, item) tuples

list(enumerate(characters))

# Use enumerate() in a for loop to get an item and its index

for index, character in enumerate(characters):
print(index, character)

# Why might you want to use enumerate?
# Example: store index positions of duplicate items

characters = ["Krillin","Goku", "Goku", "Gohan", "Piccolo",
"Krillin","Goku", "Vegeta", "Gohan", "Piccolo",
"Piccolo","Goku", "Vegeta", "Goku", "Piccolo"]

character_map = {character:[] for character in set(characters)}

print(character_map)

# Use enumerate to store the index for each occurence
for index, character in enumerate(characters):
character_map[character].append(index)

character_map
How to Use enumerate() in PythonNaruto Power Levels Over Time (1 Second = 1 Episode)Introduction to R: Decision Treesdplyr: PipesPython for Data Analysis: Probability Distributionsdlpyr: rename and arrangestringr: String MatchingHow to Combine and Split Strings in RPython for Data Analysis: Working With Text DataPython for Data Analysis: Random ForestsPython Programming Practice: LeetCode #16   3Sum ClosestHow to Plot Functions in R
DataDaft |

How to Use enumerate() in Python

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER