how to print a list on one horizontal line, in python 3

12,297 views
Skip to first unread message

Lei

unread,
Dec 19, 2014, 6:25:43 AM12/19/14
to spyd...@googlegroups.com
Hi experts,

I am using python 3. 

I wrote in IPython console in Spyder

for elem in [10, 20, 25, 27, 28.5]:

    print(elem),


But the results are


10

20

25

27

28.5


As I learned, the comma in the syntax "print(elem)," will prevent the use of newline character. Why does it not work here?

How can I make the results in one line? Like


10 20 25 27 28.5


I know I can use 

print([elem for elem in [10, 20, 25, 27, 28.5]])


and I get
[10, 20, 25, 27, 28.5]

But this is not something I want. What I want is, with the first codes, why the results are in vertical line, even with the comma?

Thanks!


Denis Lisov

unread,
Dec 19, 2014, 8:55:57 AM12/19/14
to spyd...@googlegroups.com
Hello Lei,


> As I learned, the comma in the syntax "print(elem)," will prevent the use of
> newline character. Why does it not work here?

This method of suppressing the newline is outdated. It was used with the
print statement in python2, but in python3 print is a function, so the comma
does not work anymore.

> How can I make the results in one line?

You can do almost the same thing you tried to do

for elem in [10, 20, 25, 27, 28.5]:
print(elem, end=" ")

or just use the tuple unpacking when calling print

print(*[10, 20, 25, 27, 28.5])

Adrian Klaver

unread,
Dec 19, 2014, 9:28:32 AM12/19/14
to spyd...@googlegroups.com
On 12/19/2014 03:25 AM, Lei wrote:
> Hi experts,
>
> I am using python 3.
>
> I wrote in IPython console in Spyder
>
> for elem in [10, 20, 25, 27, 28.5]:
>
> print(elem),
>
>
> But the results are
>
>
> 10
>
> 20
>
> 25
>
> 27
>
> 28.5
>
>
> As I learned, the comma in the syntax "print(elem)," will prevent the
> use of newline character. Why does it not work here?

That works in Python 2:

In [1]: for elem in [10, 20, 25, 27, 28.5]:
...: print(elem),
...:
10 20 25 27 28.5

but not using the print function from Python3:

In [3]: from __future__ import print_function

In [4]: for elem in [10, 20, 25, 27, 28.5]:
print(elem),
...:
10
20
25
27
28.5

To get it to work do:

In [5]: for elem in [10, 20, 25, 27, 28.5]:
print(elem, end=',')
...:
10,20,25,27,28.5,


>
> How can I make the results in one line? Like
>
>
> 10 20 25 27 28.5
>
>
> I know I can use
>
> print([elem for elem in [10, 20, 25, 27, 28.5]])
>
>
> and I get
> [10, 20, 25, 27, 28.5]
>
> But this is not something I want. What I want is, with the first codes,
> why the results are in vertical line, even with the comma?
>
> Thanks!
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "spyder" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to spyderlib+...@googlegroups.com
> <mailto:spyderlib+...@googlegroups.com>.
> To post to this group, send email to spyd...@googlegroups.com
> <mailto:spyd...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/spyderlib.
> For more options, visit https://groups.google.com/d/optout.


--
Adrian Klaver
adrian...@aklaver.com

Lei

unread,
Dec 19, 2014, 11:07:07 AM12/19/14
to spyd...@googlegroups.com
Thank you very much!

Lei

unread,
Dec 19, 2014, 11:07:21 AM12/19/14
to spyd...@googlegroups.com
Thank you very much!

Rajat

unread,
Jun 5, 2018, 6:05:33 PM6/5/18
to spyder


On Friday, December 19, 2014 at 4:55:43 PM UTC+5:30, Lei wrote:
Hi experts,

I am using python 3. 

for elem in [10, 20, 25, 27, 28.5]:

    print(elem),


But the results are


10

20

25

27

28.5


As I learned, the comma in the syntax "print(elem)," will prevent the use of newline character. Why does it not work here?

How can I make the results in one line? Like


10 20 25 27 28.5


I know I can use 

print([elem , end=',')

 but doing this i am getting commas i dont need commas
10 ,20, 25 ,27 ,28.5,
please help 
 

sagar ninave

unread,
Jun 6, 2018, 10:05:03 AM6/6/18
to spyd...@googlegroups.com



--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+unsubscribe@googlegroups.com.
To post to this group, send email to spyd...@googlegroups.com.
Visit this group at https://groups.google.com/group/spyderlib.

Ravinder Daudhria

unread,
Apr 28, 2019, 9:30:34 PM4/28/19
to spyder

In python 3

for elem in [10, 20, 25, 27, 28.5]:

    #instered of using this print(elem),

     #use

       print(elem,end='  ') #space in inverted comma.

thanks

Reply all
Reply to author
Forward
0 new messages