Iterate through a dictionary of lists one "line" at a time
Steve Holden
steve at holdenweb.com
Wed Apr 18 14:52:02 EDT 2007
wswilson wrote:
> 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!
>
>>> listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}
>>> for (id, name) in zip(listing['id'], listing['name']):
... print id, name
...
a Joe
b Jane
c Bob
>>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list
mailing list