Lambdas Functions in Python

Lambdas are one line functions. They are also known as anonymous functions in some other languages. You might want to use lambdas when you don’t want to use a function twice in a program. They are just like normal functions and even behave like them.

def square(x):
    return x * x
# But Lamdas are totally different

square = lambda x:x*x
print(square(2))
# You can also store lamda inside of a variable
# In this case we are passing x as an parameter. Basically they are short and # anonymous functions
# Lambdas don't have a name'

Map() function in Lambdas

nums = [1,2,3,4,5]
x = map(lambda x:x*x,nums)
print(list(x))
# This shows how map() function works. We are passing lamda function that will square each number in the list nums

filter() Function

x = [1,2,3,4,5,6,7,8]
names = ['alex','john','aneesa','aidan','amina','jill','jackson']
data = [
  {'username' : 'alex_1'},
  {'username' : 'axis_07'},
  {'username' : 'aaa'},
  {'username' : 'asthetic'},
  {'username' : 'bob'},
  {'username' : 'jason_07'},
  {'username' : 'razer_07'},
  {'username' : 'pythonista'}
]
# A function that will print the usernames with more than 4 characters
usernames = list(filter(lambda u:len(u['username']) > 4 , data))
for name in usernames:
  print(name['username'])

# A new list with the multiples of two and less than 10
print('\n')
new_list = list(map(lambda num: num*2, filter(lambda num: num < 6   ,x)))
print(new_list)
print('\n')
for nums in new_list:
  print(nums)

# filter names starting with J and adding them to group 1 and names starting with a goes to group_2

group_1 = filter(lambda n:n[0]=='j',names)
print(list(group_1))

group_2 = filter(lambda n:n[0]=='a',names)
print(list(group_2))

# We can also use the list comprehension but it's better to understand how the map() and filter() works

Any() and all() built in function

list = [1,2,3,4]
all(list)
#This will return true since all the values are true
# You can also check if all numbers are divisible by two in list

Lets say you want to check if every name started in the list with the character C

list = ['Casey','Charlie','Courtney','Cashmere']
x = [name[0] == 'C' for name in list]
print(all(x))
#This will return True since every name starts with C