printing list

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

    #1

    printing list

    How do you print elements of the list in one line?

    alist = [1, 2, 5, 10, 15]

    so it will be like this:
    1, 2, 5, 10, 15

    because if I use this code

    for i in alist:
    print i

    the result would be like this

    1
    2
    5
    10
    15

    Thanks.

  • Gary Herron

    #2
    Re: printing list

    compboy wrote:
    [color=blue]
    >How do you print elements of the list in one line?
    >
    >alist = [1, 2, 5, 10, 15]
    >
    >so it will be like this:
    >1, 2, 5, 10, 15
    >
    >because if I use this code
    >
    >for i in alist:
    > print i
    >
    >the result would be like this
    >
    >1
    >2
    >5
    >10
    >15
    >
    >Thanks.
    >
    >[/color]

    Well, first, if you just print alist you'll get

    [1, 2, 5, 10, 15]

    which may be good enough. If that's not what you want then you can suppress the automatic RETURN that follows a print's output by adding a trailing comma to the print statement, like this

    for i in alist:
    print i,

    1 2 5 10 15

    Gary Herron





    Comment

    • Tim Chase

      #3
      Re: printing list

      compboy wrote:[color=blue]
      > How do you print elements of the list in one line?
      >
      > alist = [1, 2, 5, 10, 15]
      >
      > so it will be like this:
      > 1, 2, 5, 10, 15[/color]
      [color=blue][color=green][color=darkred]
      >>> print ', '.join(alist)[/color][/color][/color]
      1, 2, 5, 10, 15

      -tkc






      Comment

      • Raymond L. Buvel

        #4
        Re: printing list

        compboy wrote:[color=blue]
        > How do you print elements of the list in one line?
        >
        > alist = [1, 2, 5, 10, 15]
        >
        > so it will be like this:
        > 1, 2, 5, 10, 15
        >
        > because if I use this code
        >
        > for i in alist:
        > print i
        >
        > the result would be like this
        >
        > 1
        > 2
        > 5
        > 10
        > 15
        >
        > Thanks.
        >[/color]

        There are a number of ways to do it but if you want a one-liner:

        print repr(alist)[1:-1]

        will meet your spec.

        Comment

        • Mel Wilson

          #5
          Re: printing list

          Tim Chase wrote:[color=blue]
          > compboy wrote:
          >[color=green]
          >> How do you print elements of the list in one line?
          >>
          >> alist = [1, 2, 5, 10, 15]
          >>
          >> so it will be like this:
          >> 1, 2, 5, 10, 15[/color]
          >
          >[color=green][color=darkred]
          > >>> print ', '.join(alist)[/color][/color]
          > 1, 2, 5, 10, 15[/color]

          ???

          Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
          [GCC 3.3.4] on linux2
          Type "help", "copyright" , "credits" or "license" for more
          information.[color=blue][color=green][color=darkred]
          >>> a=[1,2,3,4,5]
          >>> print ', '.join (a)[/color][/color][/color]
          Traceback (most recent call last):
          File "<stdin>", line 1, in ?
          TypeError: sequence item 0: expected string, int found[color=blue][color=green][color=darkred]
          >>> print ', '.join ('%d'%x for x in a)[/color][/color][/color]
          1, 2, 3, 4, 5

          Comment

          • Dan Sommers

            #6
            Re: printing list

            On Sun, 07 May 2006 18:16:22 -0400,
            Mel Wilson <mwilson-to@sympatico.ca > wrote:
            [color=blue]
            > Tim Chase wrote:[color=green]
            >> compboy wrote:
            >>[color=darkred]
            >>> How do you print elements of the list in one line?
            >>>
            >>> alist = [1, 2, 5, 10, 15]
            >>>
            >>> so it will be like this:
            >>> 1, 2, 5, 10, 15[/color]
            >>
            >>[color=darkred]
            >> >>> print ', '.join(alist)[/color]
            >> 1, 2, 5, 10, 15[/color][/color]
            [color=blue]
            > ???[/color]
            [color=blue]
            > Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
            > [GCC 3.3.4] on linux2
            > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
            >>>> a=[1,2,3,4,5]
            >>>> print ', '.join (a)[/color][/color]
            > Traceback (most recent call last):
            > File "<stdin>", line 1, in ?
            > TypeError: sequence item 0: expected string, int found[color=green][color=darkred]
            >>>> print ', '.join ('%d'%x for x in a)[/color][/color]
            > 1, 2, 3, 4, 5[/color]

            Or one of:

            print ', '.join(str(x) for x in a)

            print ', '.join(map(str, a))

            both of which work if the list contains non-integer elements.

            Regards,
            Dan

            --
            Dan Sommers
            <http://www.tombstoneze ro.net/dan/>
            "I wish people would die in alphabetical order." -- My wife, the genealogist

            Comment

            Working...