Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70037 > unrolled thread

why i have the output of [None, None, None]

Started bylength power <elearn2014@gmail.com>
First post2014-04-10 21:54 +0800
Last post2014-04-10 07:18 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  why i have the output of [None, None, None] length power <elearn2014@gmail.com> - 2014-04-10 21:54 +0800
    Re: why i have the output of [None, None, None] Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-04-10 17:18 +0300
    Re: why i have the output of [None, None, None] Rustom Mody <rustompmody@gmail.com> - 2014-04-10 07:18 -0700

#70037 — why i have the output of [None, None, None]

Fromlength power <elearn2014@gmail.com>
Date2014-04-10 21:54 +0800
Subjectwhy i have the output of [None, None, None]
Message-ID<mailman.9135.1397138088.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

>>> x=['','x1','x2','x3','   ']
>>> x
['', 'x1', 'x2', 'x3', '   ']
>>> [print("ok") for it in x if it.strip() !=""]
ok
ok
ok
[None, None, None]

i understand there are three 'ok' in the output,but why i have the output
of [None, None, None]

[toc] | [next] | [standalone]


#70040

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-04-10 17:18 +0300
Message-ID<qotr455qndn.fsf@ruuvi.it.helsinki.fi>
In reply to#70037
length power writes:

> >>> x=['','x1','x2','x3','   ']
> >>> x
> ['', 'x1', 'x2', 'x3', '   ']
> >>> [print("ok") for it in x if it.strip() !=""]
> ok
> ok
> ok
> [None, None, None]
> 
> i understand there are three 'ok' in the output,but why i have the
> output of [None, None, None]

It's a list containing the values from three invocations of print.
You get it because you asked for it.

|>>> print("ok") == None
|ok
|True
|>>> print("ok") != None
|ok
|False
|>>> [(print("ok") or "die") for x in (1,2,3)]
|ok
|ok
|ok
|['die', 'die', 'die']
|>>> [print("ok") for x in (1,2,3)] and print("What do you want it to be?")
|ok
|ok
|ok
|What do you want it to be?
|>>> 

(That last one actually returns None to the interpreter, which
promptly does not print it.)

[toc] | [prev] | [next] | [standalone]


#70041

FromRustom Mody <rustompmody@gmail.com>
Date2014-04-10 07:18 -0700
Message-ID<757dd9bf-8207-4bdd-bafc-f35169db1a27@googlegroups.com>
In reply to#70037
This is called imperative programming:

for it in x:
...   if it.strip() != '':
...        print ("Ok")

This is called functional programming:
>>> [y for y in x if y.strip() != '']
['x1', 'x2', 'x3']

What you have is a confusion:
print is imperative
comprehension is functional
You should not mix them like that

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web