Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > formatting list -> comma separated

Reply
Thread Tools

formatting list -> comma separated

 
 
Robert
Guest
Posts: n/a
 
      07-09-2008
given d:

d = ["soep", "reeds", "ook"]

I want it to print like

soep, reeds, ook

I've come up with :

print ("%s"+", %s"*(len(d)-1)) % tuple(d)

but this fails for d = []

any (pythonic) options for this?

Robert



 
Reply With Quote
 
 
 
 
Jerry Hill
Guest
Posts: n/a
 
      07-09-2008
On Wed, Jul 9, 2008 at 3:23 PM, Robert <(E-Mail Removed)> wrote:
> given d:
> d = ["soep", "reeds", "ook"]
>
> I want it to print like
> soep, reeds, ook


use the join() method of strings, like this:
>>> d = ["soep", "reeds", "ook"]
>>> ', '.join(d)

'soep, reeds, ook'
>>> d = []
>>> ', '.join(d)

''
>>>


--
Jerry
 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      07-09-2008
On Jul 9, 8:23*pm, "Robert" <(E-Mail Removed)> wrote:
> given d:
>
> d = ["soep", "reeds", "ook"]
>
> I want it to print like
>
> soep, reeds, ook
>
> I've come up with :
>
> print ("%s"+", %s"*(len(d)-1)) % tuple(d)
>
> but this fails for d = []
>
> any (pythonic) options for this?


print ', '.join(d)

--
Paul Hankin
 
Reply With Quote
 
norseman
Guest
Posts: n/a
 
      07-10-2008

Robert wrote:
> given d:
>
> d = ["soep", "reeds", "ook"]
>
> I want it to print like
>
> soep, reeds, ook
>
> I've come up with :
>
> print ("%s"+", %s"*(len(d)-1)) % tuple(d)
>
> but this fails for d = []
>
> any (pythonic) options for this?
>
> Robert
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

================================
the old fashion way would be:

d = ["soep", "reeds", "ook"]
if len(d) > 0:
print ("%s"+", %s"*(len(d)-1)) % tuple(d)

# if d= [] then print stmnt bypassed



Steve
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comma separated list using XPath nikoromano@gmail.com XML 9 04-16-2011 02:21 PM
Re: formatting list -> comma separated (slightly different) Michiel Overtoom Python 1 07-22-2008 06:32 AM
Array to a comma Separated String Peter Rilling ASP .Net 3 07-08-2004 05:53 PM
How to format $mail_from with 2 variables separated by a comma? Jason Miles Perl 1 05-28-2004 02:48 AM
How do I loop through a comma-separated list? Christina ASP General 26 02-02-2004 06:48 PM



Advertisments