-3

I need to perform filter using custom logic. I have defined a function as follows:

def isValid(str):
 if str=='test':
  true
 else:
  false

and calling it from a filter lambda as follows:

data.filter(lambda obj: isValid(obj['str'])

This doesn't work. What am I missing?

Saqib Ali
  • 3,953
  • 10
  • 55
  • 100

1 Answers1

0

If you requirement looks like this:

data = [
    {'name': "group1", "str":"test"}, 
    {'name': "group2", "str":"validation"}, 
    {'name': "group2", "str":"test"}
]

def isValid(str):
    if str=='test':
        return True
    else:
        return False

filtered_list = list(filter(lambda obj: isValid(obj['str']), data))
print(filtered_list)


output : [{'name': 'group1', 'str': 'test'}, {'name': 'group2', 'str': 'test'}]
###Note: object having test as value of key str is filtered
Shivam Seth
  • 677
  • 1
  • 8
  • 21