Use print inside lambda

后端 未结 4 928
清歌不尽
清歌不尽 2021-01-20 02:35

I am trying to use print inside lambda. Something like that:

lambda x: print x

I understand, that in

4条回答
  •  盖世英雄少女心
    2021-01-20 03:19

    The question is about Python 2, but I ended up here from Google trying to use the print function inside a lambda in Python 3. I'm adding this answer for context for others that come here for the same.

    If you only want to see the code that works and not how I arrived there, skip to the last code sample at the bottom. I wanted to clearly document what didn't work for learning purposes.

    Desired result

    Let's suppose you want to define a lambda print_list that prints each item of a list with a newline in between.

    lst = [1, 2, 3]
    
    print_list = lambda lst: ...
    

    The desired output is:

    1
    2
    3
    

    And there should be no unused return value.

    Attempt 1 - A map doesn't evaluate the print function in Python 3

    To start, here's what doesn't work well in Python 3:

    map(print, lst)
    

    However, the output is somewhat counterintuitively not printed lines, because the map call in Python 3 returns an iterator instead of an evaluated list.

    Output:

    n/a
    

    Return value:

    
    

    Attempt 2 - Evaluate the map iterator

    You can realize the printing by passing the map result to list(...), which produces the ideal output, but has the side effect of returning a list of nulls (as evaluated in the REPL).

    list(map(print, lst))
    

    Output:

    1
    2
    3
    

    Return value:

    [None, None, None]
    

    You could workaround this by using the underscore throwaway variable convention:

    _ = list(map(print, lst))
    

    A similar approach is calling print inside a list comprehension:

    [print(i) for i in lst]
    

    I don't love these approaches because they both still generate an unused return value.

    Attempt 3 - Apply the unpacking operator to the map iterator

    Like this:

    [*map(print, [1, 2, 3])]
    

    (This still returns a list of nulls which is non-ideal.)

    In the comments above @thefourtheye suggests using a one-line for loop:

    for item in [1, 2, 3]: print(item)
    

    This works fine for most cases and avoids the side effect. Attempting to put this in a lambda throws a SyntaxError. I tried wrapping it in parens without success; though there is probably a way to achieve this, I haven't figured it out.

    (SOLUTION!) Attempt 4 - Apply the unpacking operator inside of the print call

    The answer I arrived at is to explode the list inside the print call alongside using the separator arg:

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

    Output:

    1
    2
    3
    

    This produces the intended result without a return value.

    Finally, let's wrap it up in a lambda to use as desired:

    print_list = lambda lst: print(*lst, sep='\n')
    print_list([1, 2, 3])
    

    This was the best solution for my use case in Python 3.

    Related questions

    • Why map(print, a_list) doesn't work?
    • Print doesnt print when it's in map, Python

提交回复
热议问题