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?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <karim.liateni@free.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'lines.': 0.05; 'case)': 0.09; 'dict': 0.09; 'iterate': 0.09; 'pm,': 0.10; 'this:': 0.10; 'output': 0.11; 'wrote:': 0.14; 'columns': 0.16; 'from:addr:free.fr': 0.16; 'pprint': 0.16; 'received:212.27': 0.16; 'received:212.27.42': 0.16; 'received:212.27.42.10': 0.16; 'received:free.fr': 0.16; 'received:smtpfb2-g21.free.fr': 0.16; 'subject: \n ': 0.16; 'subject:Out': 0.16; 'header:In-Reply-To:1': 0.21; '(in': 0.26; '(not': 0.28; 'subject:?': 0.29; 'import': 0.29; 'code,': 0.29; 'print': 0.31; 'cheers': 0.32; 'headers': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'lines': 0.33; 'rather': 0.34; 'normally': 0.34; 'header:User-Agent:1': 0.35; 'file:': 0.35; 'subject:What': 0.35; 'similar': 0.37; 'something': 0.37; 'pretty': 0.37; 'but': 0.38; 'data': 0.38; 'subject:: ': 0.38; 'received:192': 0.38; 'to:addr:python.org': 0.39; 'header': 0.40; 'order': 0.62; 'costs': 0.65; 'series': 0.66; 'alternative': 0.71; '05:29': 0.84; 'header1': 0.84; 'so:': 0.84; 'subject:\t': 0.93
Date Tue, 14 Jun 2011 20:28:46 +0200
From Karim <karim.liateni@free.fr>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110516 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
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>
In-Reply-To <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.228.1308076147.11593.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 82.94.164.166
X-Trace 1308076147 news.xs4all.nl 49180 [::ffff:82.94.164.166]:57675
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7625

Show key headers only | 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