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

J

John

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
 
A

Andres Rosado-Sepulveda

John said:
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: (e-mail address removed)
ICQ: 66750646
AIM: pantear
Homepage: http://andres980.tripod.com/

Every election is a sort of advance auction sale of stolen goods.
-- H.L. Mencken
 
J

John Matthews

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:

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

John
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top