formatting list -> comma separated

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    formatting list -> comma separated

    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



  • Jerry Hill

    #2
    Re: formatting list -> comma separated

    On Wed, Jul 9, 2008 at 3:23 PM, Robert <rw.segaar@xs4a ll.nlwrote:
    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

    Comment

    • Paul Hankin

      #3
      Re: formatting list -&gt; comma separated

      On Jul 9, 8:23 pm, "Robert" <rw.seg...@xs4a ll.nlwrote:
      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

      Comment

      • norseman

        #4
        Re: formatting list -&gt; comma separated


        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
        >
        >
        >
        --

        >
        =============== =============== ==
        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
        norseman@hughes .net

        Comment

        Working...