List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
Syntax:-
newlist = [expression for item in iterable if condition == True]
Example:-
Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.Without list comprehension you will have to write a for statement with a conditional test inside:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
With list comprehension you can do all that with only one line of code:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
newlist = [x for x in fruits if "a" in x]
print(newlist)
Python Lambda
A lambda function is a small anonymous function.A lambda function can take any number of arguments, but can only have one expression.
Syntax:-
lambda arguments : expression
Example:-
x = lambda a : a + 10
print(x(5))
print(x(5))
Python map() function
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Syntax:-
map(fun, iter)
Parameters:
- fun: It is a function to which map passes each element of given iterable.
- iter: It is iterable which is to be mapped.
Example:-
def addition(n):
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
map() with Lambda Expressions
We can also use lambda expressions with map to achieve above result. In this example, we are using map() with lambda expression.
numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))
result = map(lambda x: x + x, numbers)
print(list(result))
Python filter() Function
The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not.
Syntax:-
filter(function, iterable)
Parameter Values
- function A Function to be run for each item in the iterable
- iterable The iterable to be filtered
Example:-
ages = [5, 12, 17, 18, 24, 32]
def myFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, ages)
for x in adults:
print(x)
def myFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, ages)
for x in adults:
print(x)
reduce() in Python
In Python, reduce() is a built-in function that applies a given function to the elements of an iterable, reducing them to a single value.
Syntax:-
functools.reduce(function, iterable[, initializer])
Example:-
from functools import reduce
def add(a, b):
return a + b
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = reduce(add, num_list)
print(f"Sum of the integers of num_list : {sum}")
sum = reduce(add, num_list, 10)
print(f"Sum of the integers of num_list with initial value 10 : {sum}")
def add(a, b):
return a + b
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = reduce(add, num_list)
print(f"Sum of the integers of num_list : {sum}")
sum = reduce(add, num_list, 10)
print(f"Sum of the integers of num_list with initial value 10 : {sum}")