[Tutor] priting keys and its values of a dictionary in a single row

Bill Burns billburns at pennswoods.net
Sat Apr 28 00:26:04 CEST 2007


Srinivas Iyyer wrote:
> Dear group,
> 
> I have a dictionary (huge) and there are 19K keys for
> this dictionary. The values for the could be a max 19K
> again. 
> 
> simpler version of it:
> 
> mydict=
> {'a':['apple','ant','anchor','arrow'],'b':['ball','baby',boy','bus'],'c':['cat','call']}
> 
> in a different list, i have keys:
> mlist = ['a','b','c']
> 
> I want to print in the following way:
> a,apple,ant,anchor,arrow
> b,ball,baby,boy,bus
> c,cat call
> 
> I cannot think of a simple way to do that (may be a
> mental block). could any one help me please. 
> 
> Thanks
> srini
> 

Try this:

for key in mlist: print '%s,%s' % (key, ','.join(mydict[key]))

Bill



More information about the Tutor mailing list