Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7625

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

Date 2011-06-14 20:28 +0200
From Karim <karim.liateni@free.fr>
Subject Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?
References <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.228.1308076147.11593.python-list@python.org> (permalink)

Show all headers | View raw


On 06/14/2011 05:29 PM, Zachary Dziura wrote:
> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Zachary Dziura <zcdziura@gmail.com> - 2011-06-14 08:29 -0700
  Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Terry Reedy <tjreedy@udel.edu> - 2011-06-14 12:06 -0400
    Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Zach Dziura <zcdziura@gmail.com> - 2011-06-14 10:48 -0700
      Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? MRAB <python@mrabarnett.plus.com> - 2011-06-14 19:37 +0100
      Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Terry Reedy <tjreedy@udel.edu> - 2011-06-15 00:53 -0400
  Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Karim <karim.liateni@free.fr> - 2011-06-14 20:28 +0200
  Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Ben Finney <ben+python@benfinney.id.au> - 2011-06-15 09:40 +1000
    Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Chris Angelico <rosuav@gmail.com> - 2011-06-15 10:08 +1000

csiph-web