Python Forum
How to transform an enumerate object into a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to transform an enumerate object into a dictionary
#7
j.crater,
did you run the code you posted. you can't print(my_Dictionary), its a dict.

Here's another way to do it:
    aString = "Mississippi"
    my_dictionary = dict()
    enumerated_string = enumerate(aString)
    print("enumerated_string:\t" + str(enumerated_string))
    for element in enumerated_string:
        print(str(element))
        my_dictionary[element[0]] = element[1]
    # rather than this, since you don't really need the names of the tuple elements:
    #for number, letter in enumerated_string:
    #    my_dictionary[number] = letter
    print("my_dictionary:\t" + str(my_dictionary))
Reply


Messages In This Thread
RE: How to transform an enumerate object into a dictionary - by kmcollins - Feb-11-2018, 11:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Alternative of this mini code without Enumerate and join function. Ace4Benji 2 2,517 Mar-09-2020, 08:22 PM
Last Post: micseydel
  Code Review: Range and Enumerate lummers 2 2,144 Jan-11-2020, 12:40 AM
Last Post: lummers
  Help using a pre-code for Discrete Fourier Transform ToucheP 0 1,690 Mar-28-2019, 06:18 AM
Last Post: ToucheP
  enumerate and output control atux_null 7 5,209 Oct-24-2017, 06:50 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020