Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'lines.': 0.05; 'terry': 0.07; 'adjusting': 0.09; 'assumed': 0.09; 'case)': 0.09; 'dict': 0.09; 'iterate': 0.09; 'length.': 0.09; 'line:': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'value:': 0.09; 'this:': 0.10; 'output': 0.11; '>>>': 0.12; 'skip:[ 20': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'arbitrary.': 0.16; 'length,': 0.16; 'pad': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:Out': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'column': 0.22; 'say,': 0.22; 'code.': 0.22; 'assumes': 0.23; 'pair': 0.23; 'code': 0.24; 'skip:l 30': 0.25; 'specify': 0.25; '(in': 0.26; 'subject:?': 0.29; 'strings,': 0.30; 'table.': 0.30; 'print': 0.31; 'headers': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'lines': 0.33; 'rather': 0.34; 'first.': 0.34; 'normally': 0.34; 'header:User- Agent:1': 0.35; 'subject:What': 0.35; 'actual': 0.36; 'similar': 0.37; 'something': 0.37; 'assuming': 0.37; 'received:org': 0.38; 'data': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'printed': 0.39; 'header:Mime-Version:1': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'header': 0.40; 'needed.': 0.40; 'more': 0.60; 'order': 0.62; 'series': 0.66; 'here': 0.66; 'discovered': 0.69; '11:29': 0.84; 'dict,': 0.84; 'header1': 0.84; 'num': 0.84; 'print()': 0.84; 'so:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Date: Tue, 14 Jun 2011 12:06:18 -0400 References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308067590 news.xs4all.nl 49041 [::ffff:82.94.164.166]:52140 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7618 On 6/14/2011 11:29 AM, 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 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() # 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. -- Terry Jan Reedy