help needed with dictionary

L

lee

hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....

i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']
 
B

Bruno Desthuilliers

lee a écrit :
hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....

i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']

Please post the minimal *running* code exhibiting your problem. The
above snippet raises a NameError about person_name on line 3.

Anyway, looking at my crystal ball, I'd say that you're (re)binding the
variable 'kev' to a new empty dict each time.

Here's a working snippet:

import sys

kev = {}
try:
while True:
answer = raw_input("type a name :")
answer = answer.strip()
if answer:
try:
kev['name'].append(answer)
except KeyError:
kev['name'] = [answer]
print "name is now : %s" % " ".join(kev['name'])
print ""

except KeyboardInterrupt:
sys.exit("bye")
 
L

lee

hi,
thank you, ur code was helpful.... :)




lee a écrit :


hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....
i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']

Please post the minimal *running* code exhibiting your problem. The
above snippet raises a NameError about person_name on line 3.

Anyway, looking at my crystal ball, I'd say that you're (re)binding the
variable 'kev' to a new empty dict each time.

Here's a working snippet:

import sys

kev = {}
try:
while True:
answer = raw_input("type a name :")
answer = answer.strip()
if answer:
try:
kev['name'].append(answer)
except KeyError:
kev['name'] = [answer]
print "name is now : %s" % " ".join(kev['name'])
print ""

except KeyboardInterrupt:
sys.exit("bye")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top