#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    A doubt in Python's list comprehensions code.


    Here's my code (in Python 3.0).
    Code:
    >>> a = ['puppy','luppy','pupil']  #assigning a to any random list of string
    >>> [line for line in a if line[0] == 'p'] # printing the strings containing the first character 'p'
    ['puppy','puil'] # I get a list of two strings
    But when I type:
    Code:
    >>> a = ['puppy','luppy','pupil'] #same as before
    >>> [print(line) for line in a if line[0] == 'p'] #same as before
    'puppy'
    'pupil'
    [None, None]
    Why am I getting those two Nones when use the print built-in?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    print('any','valid','arguments','returns','None')


    [None for word in 'No thyme for love.'.split()]

    [None for word in ('A', 'stitch', 'in', 'thyme?', 'Nein!')]
    Last edited by b49P23TIvg; March 28th, 2013 at 01:45 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by Akshat1
    Why am I getting those two Nones when use the print built-in?
    Functions in python return None unless explicitly instructed to do otherwise. You can test it yourself within the interpreter by an example

    Code:
    >>>
    >>> returnedVal = print("some message")
    some message
    >>>
    >>> if (returnedVal):
    ...     print("The returned value is not None")
    ... else:
    ...     print("The returned value is None")
    ...
    The returned value is None
    >>>
    >>>
    Regards,
    Dariyoosh
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by dariyoosh
    Functions in python return None unless explicitly instructed to do otherwise. You can test it yourself within the interpreter by an example

    Code:
    >>>
    >>> returnedVal = print("some message")
    some message
    >>>
    >>> if (returnedVal):
    ...     print("The returned value is not None")
    ... else:
    ...     print("The returned value is None")
    ...
    The returned value is None
    >>>
    >>>
    Regards,
    Dariyoosh
    Sorry, I didn't get that, may you be more clear?
    That if statement tests whether the variable is true or not, but object assinged to the vaiable is indeed true since it isn't an empty string.
    Also, I can't get how that explains my question...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by Akshat1
    That if statement tests whether the variable is true or not, but object assinged to the vaiable is indeed true since it isn't an empty string.
    For Python, several different things stand for logical false: the value False, numerical values equal to 0, empty strings (as you said), and the value None. The return value of print() is None.

    May I also point out a slight thought mistake in your first posting:

    Code:
    >>> [line for line in a if line[0] == 'p'] # printing the strings containing the first character 'p'
    Contrary to what you say in the comment, the list comprehension does NOT print the strings. Instead it creates a list of elements whose first character is “p”. When run interactively, the environment prints out this list because you haven’t specified anything else to do with the value.

    OTOH, when run this way:

    Code:
    >>> [print(line) for line in a if line[0] == 'p']
    ...you are creating a list where the return value of print(), i.e., None, is added for each string whose first characters is “p”. As a side effect, print() prints these strings. The resulting value, or list of None’s, is then printed by the environment.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    print()
    returns None.

    None tests false.

    You made a list of the return value of print.

    You made a list of None .
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480

    Comments lie!


    Code:
    >>> [print(line) for line in a if line[0] == 'p'] #same as before
    Python ignores your comment when computing your expression.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by Akshat1
    Sorry, I didn't get that, may you be more clear?
    That if statement tests whether the variable is true or not, but object assinged to the vaiable is indeed true since it isn't an empty string.
    No, the object assigned to the variable is the return value of the print function. Which is None. Printing something is very, very different from returning it.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by SuperOscar
    For Python, several different things stand for logical false: the value False, numerical values equal to 0, empty strings (as you said), and the value None. The return value of print() is None.

    May I also point out a slight thought mistake in your first posting:

    Code:
    >>> [line for line in a if line[0] == 'p'] # printing the strings containing the first character 'p'
    Contrary to what you say in the comment, the list comprehension does NOT print the strings. Instead it creates a list of elements whose first character is “p”. When run interactively, the environment prints out this list because you haven’t specified anything else to do with the value.

    OTOH, when run this way:

    Code:
    >>> [print(line) for line in a if line[0] == 'p']
    ...you are creating a list where the return value of print(), i.e., None, is added for each string whose first characters is “p”. As a side effect, print() prints these strings. The resulting value, or list of None’s, is then printed by the environment.
    OK, I got it. Thanks

IMN logo majestic logo threadwatch logo seochat tools logo