trying to understand dictionaries

Alan G Isaac alan.isaac at gmail.com
Fri Jun 12 14:32:57 CEST 2009


On 6/12/2009 7:51 AM khemeia at gmail.com apparently wrote:
> d = {'fname': [], 'ename': []}
> name1 = 'ricky'
> name2 = 'martin'
> d['fname'].append(name1)
> d['ename'].append(name2)
> 
> name1 = 'britney'
> name2 = 'spears'
> d['fname'].append(name1)
> d['ename'].append(name2)
> 
> 
> This gives me:
> {'ename': ['martin', 'spears'], 'fname': ['ricky', 'britney']}


Trying to stick close to your example,
reverse the use of lists and dicts.

worst_musicians = list()
entry = dict(fname='ricky',lname='martin')
worst_musicians.append(entry)
entry = dict(fname='britney',lname='spears')
worst_musicians.append(entry)

for musician in worst_musicians:
    print "%(fname)s %(lname)s"%musician

hth,
Alan Isaac



More information about the Python-list mailing list