By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Beginning programmer question - How to print a list in a different format

P: n/a
Hi.

I have a dictionary thus: (wraps)

p1hand =
{'b12':b12,'d12':d12,'d23':d23,'n12':n12,'n23':n23 ,'n34':n34,'p12':p12,'
p23':p23,'p34':p34,'p45':p45}

I set a variable called p1handlist so that I can print a list of the
keys to the screen:

p1handlist = p1hand.keys()
print p1handlist

and the output looks like this:

['b12', 'd12', 'd23', 'n12', 'n23', 'n34', 'p12', 'p23', 'p34', 'p45']

how can I make it so that the output looks more like this?:

b12, d12, d23, n12, n23, n34, p12, p23, p34, p45

This is for a simple text mode game I am writing and the output to the
screen is important because at times the list gets too big to print on
one line of the display.

Thanks!

John

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John wrote:
Hi.

I have a dictionary thus: (wraps)

p1hand =
{'b12':b12,'d12':d12,'d23':d23,'n12':n12,'n23':n23 ,'n34':n34,'p12':p12,'
p23':p23,'p34':p34,'p45':p45}

I set a variable called p1handlist so that I can print a list of the
keys to the screen:

p1handlist = p1hand.keys()
print p1handlist

and the output looks like this:

['b12', 'd12', 'd23', 'n12', 'n23', 'n34', 'p12', 'p23', 'p34', 'p45']

how can I make it so that the output looks more like this?:

b12, d12, d23, n12, n23, n34, p12, p23, p34, p45
You can do something like this:

toPrint = ", ".join(p1handlist)
print toPrint
This is for a simple text mode game I am writing and the output to the
screen is important because at times the list gets too big to print on
one line of the display.


You might want to split the list first and then join the sublists.

--
Andres Rosado
Email: an*****@despammed.com
ICQ: 66750646
AIM: pantear
Homepage: http://andres980.tripod.com/

Every election is a sort of advance auction sale of stolen goods.
-- H.L. Mencken
Jul 18 '05 #2

P: n/a
In article <MP************************@news-server.columbus.rr.com>,
Jo**@the.computer says...
Hi.

I have a dictionary thus: (wraps)

p1hand =
{'b12':b12,'d12':d12,'d23':d23,'n12':n12,'n23':n23 ,'n34':n34,'p12':p12,'
p23':p23,'p34':p34,'p45':p45}

I set a variable called p1handlist so that I can print a list of the
keys to the screen:

p1handlist = p1hand.keys()
print p1handlist

and the output looks like this:

['b12', 'd12', 'd23', 'n12', 'n23', 'n34', 'p12', 'p23', 'p34', 'p45']

how can I make it so that the output looks more like this?:

b12, d12, d23, n12, n23, n34, p12, p23, p34, p45

This is for a simple text mode game I am writing and the output to the
screen is important because at times the list gets too big to print on
one line of the display.

Thanks!

John

Robert & Andres,
Thanks so much for the help. As it turns out:
print ', '.join(p1handlist)


does the trick! You have made me look like a genius! :)

John
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.