24
loading...
This website collects cookies to deliver better user experience
map() is a built-in Python function that takes in two or more arguments: a function and one or more iterables, in the form:
Syntax :- *map(callable, iterable)
map() returns an iterator - that is, map() returns a special object that yields one result at a time as needed.
if you're not fimiliar with iterator :-
refer to this post :- iterator,iterable in python
for n size of input , it will return n size output
lst_of_str_ints = ['1','2','3','4','5','6','7','8','9','10']
print("list of str ints :- ",lst_of_str_ints)
lst_of_ints = list(map(int, lst_of_str_ints))
print("list of ints :- ",lst_of_ints)
Output :-
list of str ints :- ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
list of ints :- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lst_of_str_ints = ['1','2','3','4','5','6','7','8','9','10']
print("list of str ints :- ",lst_of_str_ints)
lst_of_ints = list(map(lambda x:int(x), lst_of_str_ints))
print("list of ints :- ",lst_of_ints)
Output :-
list of str ints :- ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
list of ints :- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temps_in_celsius = [0, 22.5, 40, 100]
print("temprature in celsius :-",temps_in_celsius)
temps_in_fahrenheit = list(map(lambda celsius : (9/5)*celsius + 32, temps_in_celsius))
print("temprature in fahrenheit :-",temps_in_fahrenheit)
Output :-
temprature in celsius :- [0, 22.5, 40, 100]
temprature in fahrenheit :- [32.0, 72.5, 104.0, 212.0]
names = ['Ram','Shyam','Abhiansh','Raven','Marcus']
print("Original list :-",names)
upper_cased_names = list(map(lambda x : x.upper(), names))
print("Upper case name :-",upper_cased_names)
Output :-
Original list :- ['Ram', 'Shyam', 'Abhiansh', 'Raven', 'Marcus']
Upper case name :- ['RAM', 'SHYAM', 'ABHIANSH', 'RAVEN', 'MARCUS']
filter(function, iterable)( offers a efficient way to filter out all the elements of an iterable, based on if a condition evaluates to treu
**Syntax :- *filter(callable, iterable)*
It takes first argument as function which evaluates to some boolean condition (True or False), second argument as iterable
The element will only be included if the function return **
for n size of input , it will return n-m size output
where m is the no. of element for which the function evaluates to False
lst = [12, 43.42, 'hello', [1,3,4], 54,92, (1,2), 12.22, 9]
print("Orignal list of object without filteration :- ",lst)
lst_of_ints = list(filter(lambda x:type(x)== int, lst))
print("Updated list of ints :- ",lst_of_ints)
Output :-
Orignal list of object without filteration :- [12, 43.42, 'hello', [1, 3, 4], 54, 92, (1, 2), 12.22, 9]
Updated list of ints :- [12, 54, 92, 9]
list_of_ints = [1,2,3,4,5,6,7,8,9,10]
print("Orignal list of ints :- ",list_of_ints)
list_of_even_ints = list(filter(lambda x: x % 2 ==0, list_of_ints))
print("Updated list of only even ints :- ",list_of_even_ints)
Output :-
Orignal list of ints :- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Updated list of only even ints :- [2, 4, 6, 8, 10]
import re
list_of_emails = ['[email protected]',
'[email protected]',
'luebkehotmail.com',
'fmergeslive.com',
'[email protected]',
'[email protected]',
'[email protected]',
'catalogsbcglobal.net',
'[email protected]',
'[email protected]',
'rsteinericloud.com',
'[email protected]']
email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
print("Orignal list of emails :- ",list_of_emails,"\n")
valid_emails = list(filter(lambda email:re.match(email_regex, email), list_of_emails))
print("Updated list of valid emails :- ",valid_emails)
Output :-
Orignal list of emails :- ['[email protected]', '[email protected]', 'luebkehotmail.com', 'fmergeslive.com', '[email protected]', '[email protected]', '[email protected]', 'catalogsbcglobal.net', '[email protected]', '[email protected]', 'rsteinericloud.com', '[email protected]']
Updated list of valid emails :- ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
from functools import reduce
lst =[47,11,42,13]
print("Original list :- ",lst)
print("reduced value :- ",reduce(lambda x,y: x+y,lst))
Output :-
Original list :- [47, 11, 42, 13]
reduced value :- 113
45 11 42 13
\ / / /
\ / / /
58 / /
\ / /
\ / /
100 /
\ /
\ /
\ /
113
numbers at index 0 and 1 first added
then their sum added with value at index 3
then their sum added with value at index 4
then after completing the sequence
we the reduced value :- 113
from functools import reduce
lst =[47,11,42,13]
print("Original list :- ",lst)
print("reduced value :- ",reduce(lambda x,y: x*y,lst))
Output :-
Original list :- [47, 11, 42, 13]
reduced value :- 282282
The function zip() makes an iterator that combines elements from each of the iterables
Syntax :- zip(iter1, iter2, iter3, iter4....)
zip() function return zip object which iterate over a list of tuples where tuples size is equal to the no. of list passed in the zip() function.
The iterator stops when the shortest input iterable is exhausted
i.e it will return list of tuples of size equal to the length of (minimum size *iterable among all the iterables passed in the zip() function)*
first_names = ['ram','shyam','ajay','bipin','manoj','alex']
print("list of first names :- ", first_names,"\n")
last_names = ['gupta','tiwari','yadav','rawat','desai','khan','raven']
print("list of first names :- ", last_names,"\n")
home_towns = ['ayodhya','vrindavan','bihar','jhnasi','boston','delhi','lanka']
print("list of hometowns :-" , home_towns,"\n")
persons_info = list(zip(first_names,last_names,home_towns))
print("data after using zip() function :-",persons_info)
Output :-
list of first names :- ['ram', 'shyam', 'ajay', 'bipin', 'manoj', 'alex']
list of first names :- ['gupta', 'tiwari', 'yadav', 'rawat', 'desai', 'khan', 'raven']
list of hometowns :- ['ayodhya', 'vrindavan', 'bihar', 'jhnasi', 'boston', 'delhi', 'lanka']
data after using zip() function :- [('ram', 'gupta', 'ayodhya'), ('shyam', 'tiwari', 'vrindavan'), ('ajay', 'yadav', 'bihar'), ('bipin', 'rawat', 'jhnasi'), ('manoj', 'desai', 'boston'), ('alex', 'khan', 'delhi')]
The function enumerate() return an iterates to a list of tuples , where first element of each tuple is it's corresponding index (by default is 0) and second element is the value at store at the index
Syntax :- enumerate(iterable, start)
the second argument start denotes from where the index start
i.e., if start = 4 then [(value1,4), (value2,5), (value3,6)...]
names = ['ram','shyam','ajay','bipin','manoj','alex']
names_with_indexes = list(enumerate(names))
for name in names_with_indexes:
print(name)
Output :-
(0, 'ram')
(1, 'shyam')
(2, 'ajay')
(3, 'bipin')
(4, 'manoj')
(5, 'alex')
names = ['ram','shyam','ajay','bipin','manoj','alex']
names_with_indexes = list(enumerate(names, 4))
for name in names_with_indexes:
print(name)
Output :-
(4, 'ram')
(5, 'shyam')
(6, 'ajay')
(7, 'bipin')
(8, 'manoj')
(9, 'alex')
all() are built-in functions that allow us to conveniently check for boolean matching in an iterable.
Syntax :- all(iterable)
all() will return True if all elements in an iterable are True.
lst = [True,True,False,True]
print(all(lst))
Output :-
False
lst = [True,True,True,True]
print(all(lst))
Output :-
True
lst = [True,[],[1,2,3],True]
print(all(lst))
Output :-
False
any() are built-in functions that allow us to conveniently check for boolean matching in an iterable.
Syntax :- any(iterable)
any() will return True if any elements in an iterable are True.
lst = [True,False,False,False]
print(any(lst))
Output :-
True
lst = [False,False,False,False]
print(any(lst))
Output :-
False
lst = [(),[],[1,2,3],False]
print(any(lst))
Output :-
True