What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?

Z

Zachary Dziura

I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1 Header2 Header3 Header4
1 2 3 4
5 6 7 8
9 10 11 12
 
T

Terry Reedy

I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1 Header2 Header3 Header4
1 2 3 4
5 6 7 8
9 10 11 12

You did not specify how much can be assumed about the dict and built in
to the program and how much needs to be discovered with code. Assuming
that this is not homework, here is a start:

d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'],
'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']}

arr = []
for key,value in d.items():
line = ['{:>10s}'.format(key)]
for num in value:
line.append('{:>10s}'.format(num))
arr.append(line)

for line in zip(*arr):
for item in line:
print(item, end='')
print() # newlineHeader2 Header3 Header1 Header4
2 3 1 10
5 6 4 11
8 9 7 12

For zip(*arr) to work properly, each line of arr should have the same
length, which means that either each value of d has the same length or
that you find the max length and pad lines with blanks up to the max
length. The code above assumes the first.

If the items in each value of d are not strings, more fiddling is
needed. The printed field size is also arbitrary. It needs adjusting for
the actual max length. You might want to adjust it for each key-value
pair in the dict, which is to say, each column of the resulting table.
 
Z

Zach Dziura

d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'],
    'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']}

arr = []
for key,value in d.items():
     line = ['{:>10s}'.format(key)]
     for num in value:
         line.append('{:>10s}'.format(num))
     arr.append(line)

for line in zip(*arr):
     for item in line:
         print(item, end='')
     print() # newline
 >>>
    Header2   Header3   Header1   Header4
          2         3         1        10
          5         6         4        11
          8         9         7        12

For zip(*arr) to work properly, each line of arr should have the same
length, which means that either each value of d has the same length or
that you find the max length and pad lines with blanks up to the max
length. The code above assumes the first.

If the items in each value of d are not strings, more fiddling is
needed. The printed field size is also arbitrary. It needs adjusting for
the actual max length. You might want to adjust it for each key-value
pair in the dict, which is to say, each column of the resulting table.

I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?

Otherwise, thank you for the example! This isn't homework, but I'm
working on something at work, and I was wondering how to properly
format the output from CSV files into another file. It's all a part of
an analyzer script for database tables, and the table wherein. Thank
you a bunch for the help!
 
K

Karim

I have a dict that I would like to print out in a series of columns,
rather than as a bunch of lines. Normally when you do print(dict), the
output will look something like this:

{'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1':
['1', '4', '7'], 'Header4': ['10', '11', '12']}

I can then iterate through (in this case) a list of the headers in
order to produce something similar to this:

Header1 = ['1', '4', '7']
Header2 = ['2', '5', '8']
Header3 = ['3', '6', '9']
Header4 = ['10', '11', '12']

What I want to know is how I can print out that information in a
column, where the header is the first line of the column, with the
data following underneath, like so:

Header1 Header2 Header3 Header4
1 2 3 4
5 6 7 8
9 10 11 12
Over alternative that only costs 2 lines of code, use pretty print (not
in columns but crystal clear):

import pprint
pprint.pprint(my_dict)

or in a file:
pprint.pprint(my_dict, open("output.dat", "wb"))

Cheers
karim
 
M

MRAB

On 14/06/2011 18:48, Zach Dziura wrote:
[snip]
I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?
[snip]
The * in the argument list of a function call unpacks the following
list as arguments for the call, for example, zip(*[0, 1, 2]) becomes
zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...).

There's also **, which unpacks a dict as keyword arguments.
 
C

Chris Angelico

I'm glad you got some good replies. It probably reflects badly on me
that my first thought was <URL:http://bash.org/?5804>.

Well *OBVIOUSLY* the difference is that that snippet is referring to
"Ms Access", and on this list we're working with "Montgomery Python",
and as we all know, women simply cannot do these things.

Chris Angelico
/me ducks the slings and arrows of outrageous sexism
 
T

Terry Reedy

On 14/06/2011 18:48, Zach Dziura wrote:
[snip]
I just have one quick question. On the line where you have zip(*arr),
what is the * for? Is it like the pointer operator, such as with C? Or
is it exactly the pointer operator?
[snip]
The * in the argument list of a function call unpacks the following
list as arguments for the call, for example, zip(*[0, 1, 2]) becomes
zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...).

There's also **, which unpacks a dict as keyword arguments.

* and ** in a function call, which distribute arguments,
are essentially the inverse of * and ** in function definitions,
where they say to collect arguments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top