Filter()
The filter function is used to filter elements from an iterable according to a condition. It accepts two parameters: a function and an iterable. The function must return a boolean value, which will be used to filter the iterable. For example, you can use the following code to filter all even numbers from a list.
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat filter.py
#!/usr/bin/python3
numbers = [1,2,3,4,5,6,7,8,9]
even_numbers = list(filter(lambda x:x % 2 ==0, numbers))
print(even_numbers)
odd_numbers = list(filter(lambda x:x % 2 == 1, numbers))
print(odd_numbers)
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 filter.py
[2, 4, 6, 8]
[1, 3, 5, 7, 9]
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$
Reduce()
The retuce() function applies a function to all elements of an iterable and
returns a single value. Because it is a component of the functools module, it
must be imported before use. For example, you can use the following code to
find the product of all elements in a list.
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat reduce.py
#!/usr/bin/python3
from functools import reduce
numbers = [1,2,3,4,5]
product = reduce(lambda x,y: x*y, numbers)
print(product)
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 reduce.py
120
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$
isinstance()
The isinstance function is used to check if an object is an instance of a certain class or a subclass. It takes two argument: an object and a class. For example, you can use the following code to check if a variable is a list.
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat isinstance.py
#!/usr/bin/python3
x = [1,2,3]
result = isinstance(x, list)
print(result)
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ sudo python3 isinstance.py
True
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat isinstance.py
#!/usr/bin/python3
x = (1,2,3)
result = isinstance(x, list)
print(result)
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ sudo python3 isinstance.py
False
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$
zip()
The zip function is used to combine two or more list into a single list of tuples. For example you can use the following two lists.
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ cat zip.py
#!/usr/bin/python3
list1 = [1,2,3,4,5]
list2 = [6,7,8,9]
print(list(zip(list1, list2)))
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$ python3 zip.py
[(1, 6), (2, 7), (3, 8), (4, 9)]
┌──(hackerboy㉿KumarAtulJaiswal)-[~/Desktop/python/blogger-content]
└─$
map()
The map() function is used to apply a function to all elements of an iterable. For example, you can use the following code to square all elements of a list.
numbers = [1, 2, 3]
numbers = [1, 2, 3, 4]


