Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List comprehension returning values plus [None, None, None], why? [duplicate]

Im studying comprehensions. I get the print(x) part (i think. It prints the value of x that passes the 'in' test) but why is it also returning a list of None afterward?

>>> g
['a', 'x', 'p']

>>> [print(x) for x in g]
a
x
p
[None, None, None] #whats this? 
like image 663
jason Avatar asked Aug 12 '13 17:08

jason


People also ask

Can list comprehension return two values?

2 List Comprehension. This has two drawbacks: You can only look for one value at a time. It only returns the index of the first occurrence of a value; if there are duplicates, you won't know about them.

What do list comprehensions return?

List comprehensions are used for creating new lists from other iterables. As list comprehensions return lists, they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

Are list comprehensions memory efficient than generator comprehensions?

In terms of syntax, the only difference is that you use parenthesis instead of square brackets. The type of data returned by list comprehensions and generator expressions differs. The main advantage of generator over a list is that it take much less memory. We can check how much memory is taken by both types using sys.

Do list comprehensions run faster?

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration. This is slow. Side note: It would even be worse if it was a Numpy Array and not a list.


4 Answers

print is a function (in Python3). It prints something to the screen, but returns None.

In Python2, print is a statement. [print(x) for x in g] would have raised a SyntaxError since only expressions, not statements, can be used in list comprehensions. A function call is an expression, which is why it is allowed in Python3. But as you can see, it is not very useful to use print in a list comprehension, even if it is allowed.

like image 97
unutbu Avatar answered Sep 20 '22 03:09

unutbu


You use a list comprehension to print the items in the list, and then the list itself is printed. Try assigning the list to a variable instead.

>>> g
['a', 'x', 'p']

>>> x = [print(x) for x in g]
a
x
p
#

Now the list is in x and isnt printed. The list is still there...

>>> print(x)
[None, None, None]
>>> x
[None, None, None]
like image 42
tdelaney Avatar answered Sep 23 '22 03:09

tdelaney


[print(x) for x in g]

is equivalent to:

l = []
for i in g:
    l.append(print(i))
return l

Print does the printing stuff, so you see the a, x and p, but it return None so the list you get in the end is [None, None, None]

like image 43
Viktor Kerkez Avatar answered Sep 23 '22 03:09

Viktor Kerkez


I think you are conflating a list comprehension with str.join method.

A list comprehension always produces a new list. Three ways list may be produced that is the same as g

>>> [x for x in 'axp']
['a', 'x', 'p']
>>> [x[0] for x in ['alpha', 'xray', 'papa']]
['a', 'x', 'p']
>>> list('axp')
['a', 'x', 'p']

Since g is already strings, you use join to produce a single string to print it:

>>> g=['a', 'x', 'p']
>>> ''.join(g)
'axp'

To print it, just use join with the separator desired:

>>> print('\n'.join(g))
a
x
p

You would need to use a comprehension if the elements of the list are not compatible with join by being strings:

>>> li=[1,2,3]
>>> '\n'.join(li)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>> print('\n'.join(str(e) for e in li))
1
2
3
like image 34
dawg Avatar answered Sep 20 '22 03:09

dawg