How to Use enumerate() in Python

preview_player
Показать описание
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
Рекомендации по теме
Комментарии
Автор

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

DataDaft
Автор

I've been struggling understanding enumerate for a long time, other youtubers never explain what exactly enumerate does or how it works, they just skip right over it and use it, this video explained it perfectly, thank you!

malcolmx
Автор

It's funny how python is supposed to be easier than C++ but as soon as you want to start doing something like this, C++ makes so much more sense.

myyoutubeprofile-cu
Автор

the comment "enumerate() returns a sequence of (index, item) tuples" really helped me out, for some reason all the explanations I was checking out didn't tell me what the hell it actually did, just the use cases.

Ligmamonkey
Автор

Always wondered what enumerate meant... thank you explaining that clearly. Yeah you're right I'll probly be using that all the time

dr_frankenmiller
Автор

saved my life lol thank you . very straight to the point and concise !

yyanooo
Автор

Thank you so much. It was pretty helpful.

HARSHRAJ-
Автор

Great explanation and examples. Thank you.

dalton_arcade
Автор

clean, simple and to the point ❤❤❤❤❤❤

just_living_
Автор

short and sweet, i love it. thank you so much!!

andreawu
Автор

in 4 minutes summarized what the lecturer has been trying to say in 50 minutes

hashim
Автор

Only video about enumarate() which can be found in youtube. Thank you very much..

villagebakerystudio
Автор

you should lift man, I recommend starting strength, alphadestiny, jason blaha & natural hypertrophy

Fanda
Автор

Hi @Datadaft youyr videos are awesome but not well paced in your profile please arrange all in one group and share it like you can make all predefined function of python and keep it in 1 group

aurovindsahoo
Автор

But, isn't the list already "enumerated"? Wouldn't
for x in character:
print(x, character[x])
provide the same information?

neuvocastezero
Автор

man what i typed all this exactly and i get error unhashable type 'list' on the line using append...i googled and none of it makes sense

lulprumpt
Автор

when I enumerate, how is the character getting mapped, not able to understand this -- character_map[character].append(index), it read the character in characters and saved the items to characters_map, I am stuck at character_map[character], [character ], how is it saving the the list.

sudhansumtripathy
Автор

What's the name of the site he uses?

Sam-wlvo
Автор

Could you please explain the term yield and how its used in python

ryanmanchikanti
visit shbcf.ru