printing list

Gary Herron gherron at islandtraining.com
Sun May 7 12:31:41 EDT 2006


compboy wrote:

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

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








More information about the Python-list mailing list