473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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!

Apr 18 '07 #1
4 1980
On Apr 18, 7:39 pm, wswilson <wswil...@gmail.comwrote:
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!
You can use zip(*sequence_of_sequences)
eg zip(*[[1,2,3],[4,5,6]) returns [[1,4], [2,5], [3,6]]
(You can think of it as a transposition function)

For example:

keys, item_lists = zip(*listing.iteritems())
print " ".join(keys)
for items in zip(*item_lists):
print " ".join(items)

would work.

HTH

--
Arnaud

Apr 18 '07 #2
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

Apr 18 '07 #3
On Apr 18, 2:54 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Apr 18, 7:39 pm, wswilson <wswil...@gmail.comwrote:
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!

You can use zip(*sequence_of_sequences)
eg zip(*[[1,2,3],[4,5,6]) returns [[1,4], [2,5], [3,6]]
(You can think of it as a transposition function)

For example:

keys, item_lists = zip(*listing.iteritems())
print " ".join(keys)
for items in zip(*item_lists):
print " ".join(items)

would work.

HTH

--
Arnaud
That works perfectly. Thanks so much.

Apr 18 '07 #4
wswilson a écrit :
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!

The most simple and generic I see, it could be even more simple if you
don't care of memory usage :

Let's fill a random dict :

In [118]: d=dict(zip((str(e) for e in xrange(10)), ([i**e for e in
xrange(5)] for i in xrange(10))))

In [119]: d
Out[119]:
{'0': [1, 0, 0, 0, 0],
'1': [1, 1, 1, 1, 1],
'2': [1, 2, 4, 8, 16],
'3': [1, 3, 9, 27, 81],
'4': [1, 4, 16, 64, 256],
'5': [1, 5, 25, 125, 625],
'6': [1, 6, 36, 216, 1296],
'7': [1, 7, 49, 343, 2401],
'8': [1, 8, 64, 512, 4096],
'9': [1, 9, 81, 729, 6561]}

go on :

In [146]: sorted_keys = tuple(sorted(d.keys()))

In [147]: from itertools import izip, chain

In [148]: sorted_keys = tuple(sorted(d.keys()))

In [149]: sorted_values = ( d[k] for k in sorted_keys )

In [150]: for vals in chain([sorted_keys], izip(*sorted_values)) :
.....: print '%5s'*len(d) % vals
.....:
.....:
0 1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
0 1 4 9 16 25 36 49 64 81
0 1 8 27 64 125 216 343 512 729
0 1 16 81 256 625 1296 2401 4096 6561

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
Apr 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
2
by: Andr? Roberge | last post by:
I want to "import and execute" a python program (input_test.py below) within another program (execute_test.py below) and "watch it" being executed. By watching it, I mean to display the file...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
10
by: Rafi B. | last post by:
I'm running this on a Linux/CentOs/cPanel server, trying to add caching to my PHP5 code, using ADOdb. I have two Linux servers, one of them is running perfect, the second one, just crashes without...
4
by: Hexes | last post by:
Ok i am making program wicth has from txt file put lines from first to last. well If i code it like this: ---- nFileNum = FreeFile Open "C:/VB/VB.txt" For Input As nFileNum FreeNum = 1
5
by: Nathan Sokalski | last post by:
I have an ASP.NET application which is giving the following JavaScript error: 'theForm' is undefined However, when I do a View Source one of the <scriptelements is as follows: <script...
12
by: Dom | last post by:
VB had a "line" control, just a simple line that let you separate controls without the wasted space of a Groupbox. Did CSharp drop this? Dom
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.