[Tutor] Dictionaries and multiple keys/values

David Rock david at graniteweb.com
Tue Mar 26 19:05:29 CET 2013


* Robert Sjoblom <robert.sjoblom at gmail.com> [2013-03-26 05:36]:
> 
> brittle. However, even if I was happy with that, I can't figure out
> what to do in the situation where:
> data[i+3] = 'Canadian, Pub Food' #should be two items, is currently a string.
> My problem is that I'm... stupid. I can split the entry into a list
> with two items, but even so I don't know how to add the key: value
> pair to the dictionary so that the value is a list, which I then later
> can append things to.

If your data is a list, then it will be a list in the dict.  You could
just make it so that particular key always contains a list of
characteristics, even if it's a list of only one.

>>> data = 'Canadian, Pub Food'.split(',')
>>> data
['Canadian', ' Pub Food']
>>> data = 'French'.split(',')
>>> data
['French']

Then just put the list as the value.

d['characteristics'] = data
>>> data = 'Canadian, Pub Food'.split(',')
>>> d['characteristics'] = data
>>> d['characteristics']
['Canadian', ' Pub Food']

-- 
David Rock
david at graniteweb.com


More information about the Tutor mailing list