I am getting this error I am using filter function to filter the odd values from list
Code:
l1=[1,2,4,5,6,79,100,200,500]
finnal_list=list(filter(lambda x: (x%2!=0),l1))
print(finnal_list)
l1=[1,2,4,5,6,79,100,200,500]
finnal_list=list(filter(lambda x: (x%2!=0),l1))
print(finnal_list)
CodePudding user response:
Error clearly says in your previouse cells you've assigned filter varaible. It's a bad practice to use builtin names as vafraible names. Like so example '
#old cells code
filter = "some string"
#old cells code
l1=[1,2,4,5,6,79,100,200,500]
finnal_list=list(filter(lambda x: (x%2!=0),l1))
print(finnal_list)
Throws error #
Error: TypeError: 'str' object is not callable
Fix
Simply clear that variable if you dont need it
#old cells code
filter = "some string"
#old cells code
l1=[1,2,4,5,6,79,100,200,500]
del filter
finnal_list=list(filter(lambda x: (x%2!=0),l1))
print(finnal_list)
output #
[1, 5, 79]
CodePudding user response:
It is simpler to just use a List comprehension
final_list = [x for x in l1 if x%2 != 0]
CodePudding user response:
li = [1,2,4,5,6,79,100,200,500]
print(list(filter(lambda x: x if x%2!=0 else None,l1)))