Discussion:
[spyder] how to print a list on one horizontal line, in python 3
Lei
2014-12-19 11:25:43 UTC
Permalink
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!
--
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.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
Denis Lisov
2014-12-19 13:51:39 UTC
Permalink
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])
--
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.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
Lei
2014-12-19 16:07:06 UTC
Permalink
Thank you very much!
Post by Denis Lisov
Hello Lei,
Post by Lei
As I learned, the comma in the syntax "print(elem)," will prevent the
use of
Post by Lei
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.
Post by Lei
How can I make the results in one line?
You can do almost the same thing you tried to do
print(elem, end=" ")
or just use the tuple unpacking when calling print
print(*[10, 20, 25, 27, 28.5])
--
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.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
Adrian Klaver
2014-12-19 14:10:00 UTC
Permalink
Post by Lei
Hi experts,
I am using python 3.
I wrote in IPython console in Spyder
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,
Post by Lei
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
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
--
Adrian Klaver
***@aklaver.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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
Lei
2014-12-19 16:07:21 UTC
Permalink
Thank you very much!
Post by Adrian Klaver
Post by Lei
Hi experts,
I am using python 3.
I wrote in IPython console in Spyder
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?
...: print(elem),
10 20 25 27 28.5
In [3]: from __future__ import print_function
print(elem),
10
20
25
27
28.5
print(elem, end=',')
10,20,25,27,28.5,
Post by Lei
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
<javascript:>
Post by Lei
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
--
Adrian Klaver
--
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.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...