Python: lambda function

I am relatively new to lambda functions and this helped me figure it out. I have a set of words that have an average length of 3.4 and a list of words = ['hello', 'my', 'name', 'is', 'lisa']

I want to compare the word length with the average length and print only words that exceed the average length

average = 3.4
words = ['hello', 'my', 'name', 'is', 'lisa']

print(filter(lambda x: len(words) > avg, words))

so in this case I want

['hello', 'name', 'lisa']

but instead I get:

<filter object at 0x102d2b6d8>
+4
source share
5 answers
list(filter(lambda x: len(x) > avg, words)))

filter is an iterator in python 3. You need to iterate over or call a list in a filter object.

In [17]: print(filter(lambda x: len(x) > avg, words))
<filter object at 0x7f3177246710>

In [18]: print(list(filter(lambda x: len(x) > avg, words)))
['hello', 'name', 'lisa']
+3
source
lambda x: some code

, x - , words x avg average, avg

print filter(lambda x: len(x) > average, words)
+3

. :

<filter object at 0x102d2b6d8>

. ; . - . :

print(*filter(lambda x: len(x) > average, words))

, , :

print(list(filter(lambda x: len(x) > average, words)))
+2

, , , Python 2 :

print filter(lambda x: len(x) > avg, words)

, , Python 3 filter , - , .

+2

:.

lambda x:len(x) > avg

x - . :

def aboveAverage(x):
    return len(x) > avg

aboveAverage.

:

print map(lambda x:len(x) > avg, words)
+1

All Articles