470,815 Members | 2,831 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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.

May 7 '06 #1
5 2061
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

May 7 '06 #2
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

print ', '.join(alist)

1, 2, 5, 10, 15

-tkc


May 7 '06 #3
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.


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.
May 7 '06 #4
Tim Chase wrote:
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


>>> print ', '.join(alist) 1, 2, 5, 10, 15


???

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.
a=[1,2,3,4,5]
print ', '.join (a) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found print ', '.join ('%d'%x for x in a)

1, 2, 3, 4, 5
May 7 '06 #5
On Sun, 07 May 2006 18:16:22 -0400,
Mel Wilson <mw********@sympatico.ca> wrote:
Tim Chase wrote:
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
>>> print ', '.join(alist)

1, 2, 5, 10, 15 ??? 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. a=[1,2,3,4,5]
print ', '.join (a) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found print ', '.join ('%d'%x for x in a)

1, 2, 3, 4, 5


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.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jody Gelowitz | last post: by
reply views Thread by JF Turcotte | last post: by
1 post views Thread by NickB | last post: by
reply views Thread by ALMA_J_III | last post: by
6 posts views Thread by Bill | last post: by
3 posts views Thread by William Chang | last post: by
2 posts views Thread by Brad Pears | last post: by
1 post views Thread by Glenn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.