When I do
dict = {'Alice': '7898', 'Beth': '9102', 'Cecil': '3258'}
print filter(lambda x: x, dict['Alice'])
it shows: 7898
When I do the next
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
print filter(lambda x: x=="2341", dict['Alice'])
it shows:
Why it doesn’t show True. How to get True ?
It looks like your misunderstanding is with
filter, to me. You pass a predicate function and an iterable object tofilter. It creates a new object containing those items from the first iterable for which the predicate returns a true value (not necessarilyTrueitself, just something that tests as true). The iterable in the two cases is the string'2341', which means the individual letters are tested. Of course the string'2341'is not equal to any of'2', ‘3','4', or'1'.Try it with a tuple and it’s easier to see what’s going on: