How to Use enumerate() in Python

Поділитися
Вставка
  • Опубліковано 26 січ 2025

КОМЕНТАРІ • 38

  • @DataDaft
    @DataDaft  3 роки тому +18

    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

    • @bradyb2415
      @bradyb2415 Рік тому

      character_map = {character: [i for i, char in enumerate(characters) if char == character] for character in set(characters)}

  • @malcolmx61
    @malcolmx61 2 роки тому +18

    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!

    • @SpeaksYourWord
      @SpeaksYourWord 2 роки тому +3

      And you'll always find this comment under every video

  • @nicolenguyen8940
    @nicolenguyen8940 3 роки тому +12

    i like the anime reference there

  • @Ligmamonkey
    @Ligmamonkey Рік тому

    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.

  • @dr_frankenmiller2607
    @dr_frankenmiller2607 2 роки тому +3

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

  • @myyoutubeprofile-c3u
    @myyoutubeprofile-c3u Рік тому +2

    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.

  • @zufrankhan7793
    @zufrankhan7793 2 роки тому

    I m exactly looking for this kind of video for enumerate fun. Thanks a lot

  • @HARSHRAJ-2023
    @HARSHRAJ-2023 3 роки тому +4

    Thank you so much. It was pretty helpful.

  • @yyanooo
    @yyanooo 2 роки тому

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

  • @just_living_
    @just_living_ Рік тому

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

  • @dalton-ftt
    @dalton-ftt 2 роки тому +1

    Great explanation and examples. Thank you.

  • @andreawu4547
    @andreawu4547 2 роки тому

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

  • @hashim1740
    @hashim1740 2 роки тому

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

  • @MoHazem2002
    @MoHazem2002 3 роки тому

    amzing simple explanataion!

  • @kychemclass5850
    @kychemclass5850 Рік тому

    Very nice tutorial. Tq!!! :D

  • @ultraman6950
    @ultraman6950 3 роки тому +1

    Great job.

  • @villagebakerystudio
    @villagebakerystudio Рік тому

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

  • @aminrezaei1845
    @aminrezaei1845 2 роки тому

    500th like is mine :)
    thanks pal.

  • @neuvocastezero1838
    @neuvocastezero1838 3 місяці тому

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

  • @Garth_turds
    @Garth_turds 2 роки тому

    Great video!

  • @38Fanda
    @38Fanda 2 роки тому

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

  • @lulprumpt4696
    @lulprumpt4696 9 місяців тому

    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

  • @baharehizadpanah8233
    @baharehizadpanah8233 2 роки тому

    It was really helpful, thank you

  • @sudhansumtripathy
    @sudhansumtripathy Рік тому

    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.

  • @aurovindsahoo
    @aurovindsahoo 11 місяців тому

    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

  • @ryanmanchikanti5265
    @ryanmanchikanti5265 3 роки тому +1

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

    • @DataDaft
      @DataDaft  3 роки тому

      Hey Ryan! Thanks for the idea. I'll consider that for a future video.

  • @mumble_be9453
    @mumble_be9453 2 роки тому

    Thank you!

  • @beachwave5705
    @beachwave5705 2 роки тому

    love the dbz reference

  • @Sam-wl4vo
    @Sam-wl4vo Рік тому

    What's the name of the site he uses?

  • @chitkianchan1317
    @chitkianchan1317 9 місяців тому

    what is character_map ?

  • @Howdidilose
    @Howdidilose Рік тому

    🐐🐐

  • @the_hasnat
    @the_hasnat Рік тому

    Thanks

  • @raylink1722
    @raylink1722 4 місяці тому

    Allah bless you

  • @carlosprieto2554
    @carlosprieto2554 3 роки тому

    te quiero