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
#3
It appears it is still a string?

import string
what = input ("Please tell me the string you want to use:  ")
letter = input ("What is the letter you want me to find: ")
values = {}
a = what.find (letter)
if a == -1:
    print ("not there")
else:
    e = enumerate(what)
    for x, y in e:
        values=[x] = y
        print (type(values))
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>

If I reverse x and y I get this error

values=[y] = x
TypeError: 'int' object is not iterable

I see I had errors in my code.

This code works.

import string
what = input ("Please tell me the string you want to use:  ")
letter = input ("What is the letter you want me to find: ")
values = dict()
a = what.find (letter)
if a == -1:
    print ("not there")
else:
    e = enumerate(what)
    for x, y in e:
        values[x] = y
        print (type(values))
Thank you!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Alternative of this mini code without Enumerate and join function. Ace4Benji 2 2,547 Mar-09-2020, 08:22 PM
Last Post: micseydel
  Code Review: Range and Enumerate lummers 2 2,181 Jan-11-2020, 12:40 AM
Last Post: lummers
  Help using a pre-code for Discrete Fourier Transform ToucheP 0 1,714 Mar-28-2019, 06:18 AM
Last Post: ToucheP
  enumerate and output control atux_null 7 5,289 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