List Comprehension :
List comprehensions are easy and concise way to create new lists from already existing lists.
Create a list of numbers.
nums = [1,2,3,4,5,6,7,8,9,10]
Below is one way to square the numbers in the list and create a new list.
squares = []
for n in nums:
squares.append(n**2)
print(squares)
Using list comprehension :
# create a list by iterating over nums(list) and return the square of the iterator(n).
squares = [ n**2 for n in nums ]
print(squares)
Output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Lambda Function :
It is an expression that returns a function instead of assigning it a name.
Let us see an example:
doubler = lambda x : x*2
print(doubler(5))
Output :
10
We have created a lambda function that doubles the number passed.
The doubler function above will print 10.
map :
Now suppose we want to apply some function to each element of a list.
map function will do it for us:
nums = [1,2,3,4,5,6,7,8,9,10]
squares = list(map(lambda n : n**2 , nums))
print(squares)
Output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
filter :
filter function is used to filter out results from a list.
filter function will run our filter condition on each element of the list. If the filter condition is satisfied the element is passed to the output list.

Filtering using list comprehension:
# filtering squares greater than 50
filter_squares = [n for n in squares if n>50]
print(filter_squares)
Output :
[64, 81, 100]
Filter using filter function:
# filtering squares greater than 30
filter_squares = list(filter(lambda n:n>30,squares))
print(filter_squares)
Output :
[36, 49, 64, 81, 100]
