67 Program for using lambda functions wirg filter() and map() in Python

Program:

'''Lambda functions can be used along with built-in functions like

i)filter()

ii)map()

'''



'''1)filter() function takes 2 arguments

i) lambda function

ii)list '''



# Python to illustrate filter() with lambda()

#I want sals >20000



list1=[10,20,30,40,50]

res = list(filter(lambda x:(x>20),list1))

print(res)



print("\n\n")



emps=[(101,"miller",10000,"m",11,"hyd"),

      (102,"Blake",20000,"m",12,"pune"),

      (103,"sony",30000,"f",11,"hyd"),

      (104,"sita",40000,"f",12,"pune"),

      (105,"John",50000,"m",13,"hyd")]



#Task1: I want those emps who belongs to hyd

hyd_recs = list(filter(lambda x:(x[5]=="hyd"),emps))

print(hyd_recs)

print("\n")

     

#Task2:I want male recs and female recs

male_recs = list(filter(lambda x:(x[3]=="m"),emps))

print(male_recs)

print("\n")

     

female_recs = list(filter(lambda x:(x[3]=="f"),emps))

print(female_recs)

print("\n")



#Task3 :I want those who sal>20000

res = list(filter(lambda x:(x[2]>20000),emps))

print(res)

print("\n")

     

#Task4 :I want those who belong to Hyd and sal>20000

res = list(filter(lambda x:(x[5]=="hyd" and x[2]>20000),emps))

print(res)





print("\n\n")


Output:

Program for using lambda functions wirg filter() and map() in Python

Previous
Next Post »