Skip to content Skip to sidebar Skip to footer

Python: Converting List Comprehension To Lambda Function?

I have a list below, and I'm trying to have a lambda function to retrieve 'cat' value from the list when 'prop' equals to a given string. a=[{'prop':'ABC','cat':'ABC Dir'}, {'prop

Solution 1:

If you wanted to write a function that performed that list comprehension, it would accept the original list as a parameter, along with the value to test against, and return the result of the list comprehension, like so:

defgetcats(a, value):
    return [x['cat'] for x in a if x['prop'] == value]

This is easily converted to an anonymous function (lambda) since it returns the result of a single expression.

lambda a, value: [x['cat'] for x in a if x['prop'] == value]

If you want just the first value, you can use next() with a generator expression. That way, it stops when it finds the first value.

lambda a, value: next(x['cat'] for x in a if x['prop'] == value)

Of course, there might not be any matching values. In that case, you can add the optional second parameter of next() to return None in that instance.

lambda a, value: next((x['cat'] for x in a if x['prop'] == value), None)

Solution 2:

I will take it as you don't quite get the terminologies in python. Lambda is a key word in python. If you want to define a function, just say define a function. In fact, you don't use lambda to define a named function at all.

Following code should do what you are asking for:

defbb(x):
    for i in a:
        if i['prop'] == x:
            return i['cat']
    else:
        returnNone

It's in the style guide, PEP8, that you shouldn't define a named function using lambda: https://www.python.org/dev/peps/pep-0008/

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

Solution 3:

Using list comprehensions (however generators as kindall has proposed is the most pythonic way):

a=[{'prop':'ABC','cat':'ABC Dir'}, {'prop':'DEF','cat':'DEF Dir'}]

bb = lambda prop: ' '.join([val['cat'] forvalin a ifval['prop'] == prop])

bb('ABC')

Would give you a string with all the matches:

'ABC Dir'

And here is another one that will join the matches with a blank space and if you want to pass an array too:

f = lambda prop, d: ' '.join([val['cat'] forvalin d ifval['prop'] == prop])

f('ABC',a)

Returns:

'ABC Dir'

Solution 4:

You may have to combine filter and map for a truly functional approach.

a = [{'prop':'ABC','cat':'ABC Dir'}, {'prop':'DEF','cat':'DEF Dir'}]

defbifilter(LoD, prop):
    returnmap(lambda x: x['cat'], filter(lambda d: d['prop'] == prop, LoD))

list(bifilter(a, 'ABC'))  # ['ABC Dir']

My advice would be to go with a list comprehension.

Post a Comment for "Python: Converting List Comprehension To Lambda Function?"