Iterate through a dictionary of lists one "line" at a time

Hamilton, William whamil1 at entergy.com
Wed Apr 18 15:15:22 EDT 2007


> -----Original Message-----
> From: python-list-bounces+whamil1=entergy.com at python.org
[mailto:python-
> list-bounces+whamil1=entergy.com at python.org] On Behalf Of wswilson
> Sent: Wednesday, April 18, 2007 1:39 PM
> To: python-list at python.org
> Subject: Iterate through a dictionary of lists one "line" at a time
> 
> Here is my code:
> 
> listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}
> 
> I need to output:
> 
> id name
> a Joe
> b Jane
> c Bob
> 
> I could do:
> 
> print 'id', 'name'
> for id, name in zip(listing['id'], listing['name']): print id, name
> 
> but that only works if there are two entries in the dictionary, id and
> name, and I know what they are. My problem is I don't know how many of
> these entries there will be. Thanks for any help you can give!
> 

>>> for x in xrange(len(listing['id'])):
...	print ""
...	for key in listing.keys():
...		print listing[key][x],


a Joe 
b Jane 
c Bob

---
-Bill Hamilton



More information about the Python-list mailing list