3

Assume there is a list demo_list:

[u'demo', u'trial']

Using for loop to print every element with '\n':

for i in demo_list:
    print(i)

it works as expected:

demo
trial

However, I would like to use the bracket to express this loop:

print([ i for i in demo_list]) 

But it directly prints the whole list in one line:

[u'demo', u'trial']

For the expression [print(i) for i in demo_list], it says SyntaxError: invalid syntax.

My question is:

  1. How to put every element in new lines using the bracket?
  2. What is wrong with the expression [print(i) for i in demo_list]?

Any help is appreciated.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
user2894829
  • 775
  • 1
  • 6
  • 26
  • 2
    how about `print('\n'.join(demo_list))`? Btw, the `[print(i) for i in demo_list]` does work for me but it is definitely discouraged. – Ma0 Jan 22 '19 at 07:48
  • @Ev.Kounis Thanks for your reply, but it returns `[u'demo\ntrial']`. Btw, why is the `[print(i) for i in demo_list]` discouraged? What is the disadvantage of this form? – user2894829 Jan 22 '19 at 07:50
  • _Bracket expressions_ are called list comprehensions, and it is really discouraged to use comprehensions for this purpose. You should try `join` method instead. – Ahmad Khan Jan 22 '19 at 07:52
  • Weird.. What does `type(demo_list)` return? – Ma0 Jan 22 '19 at 07:52
  • @Ev.Kounis `type(demo_list)` returns `` – user2894829 Jan 22 '19 at 07:54
  • @Ev.Kounis You can use print in list comprehension in Python 3+, but for Python 2.7 it's a statement. You can't. – knh190 Jan 22 '19 at 07:57
  • @Ev.Kounis Thanks for your reply. Why is `[print(i) for i in demo_list]` discouraged? – user2894829 Mar 15 '19 at 06:28
  • 1
    @user2894829 Because list-comprehensions are not general-purpose loop tools. They exist to create lists. In your case, you are using their built-in, list-creating loop mechanism to `print`. That's like using a fork to eat soup. – Ma0 Mar 15 '19 at 08:45

5 Answers5

1

How about using a "\n" - newline character with join after you generate the list using list comprehension (i.e. the bracket expression)?

In [29]: demo_list = [u'demo', u'trial']   

In [30]: print("\n".join([ i for i in demo_list])) 
demo
trial

In general, there no need for list comprehension if you already have the list. A simple join operation with newline char would work fine.

In [31]: print("\n".join(demo_list)) 
demo
trial
kmario23
  • 57,311
  • 13
  • 161
  • 150
1

It's called list comprehension. You cannot use a statement in a list comprehension. It's obvious if you see in this way:

[assert x for x in lst]

# same as
[print x for x in lst]

The print in Python 2.7 but not a function. So it is a SyntaxError. But this works:

l = [1,2,3]

def p(x):
    print(x)

map(p, l)
# or
[p(x) for x in l]

Reference: List Comprehension: why is this a syntax error?

knh190
  • 2,744
  • 1
  • 16
  • 30
1

You can also do this:

In [591]: from __future__ import print_function
In [592]: print(*demo_list,sep='\n')
demo
trial
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

you can :

print(*[i for i in demo_list],sep='\n')

or

print(*demo_list, sep='\n')

if you are using Python2.7 you should from __future__ import print_function.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
1

First of all, as per the error message, you are not using python 3, but 2.7x. Because your code works in python 3.

In python 2.7, print is not a function, but a statement whose format is print [,]*. (square brackets indicates optional arguments).

Also list comprehension is a way to create a list. You may see [None, None] at the end, so better you assign it to a variable.

modify your list comprehension to:

def print_stmt(x):
    print x

a = [print_stmt(x) for x in ["demo", "trial"]]

This will print as:

demo

trial

dhirajforyou
  • 432
  • 1
  • 4
  • 11