Data Structure Question

Rainer Deyke root at rainerdeyke.com
Sun Jun 17 12:42:29 EDT 2001


"Mike Johnson" <ffp_randjohnson at yahoo.com> wrote in message
news:20010617.090337.1987231011.7258 at behemoth.miketec.com...
> I was hoping to do something like:
>
> diction['thekey']=value
>
> in a loop. The key and the value are not always unique, so 'diction' ends
> up dropping some information.
>
> On thing that did work was to embed a nested dictionary into a list.
>
> list=[]
> diction={}
>
> diction['key']='somedata'
> list.append(diction)
> diction={}
> (repeat)
>
> But... This makes it a terrible pain to search and count keys....

Try the other way around, i.e. a dictonary of lists:

key_value_pairs = [('key1', 'value1'), ('key2', 'value2'), ('key2',
'value3')]

dict = {}
for key, value in key_value_pairs:
  dict.setdefault(key, []).append(value)


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list