Python map( ), filter( ) and range( ) function

 

python map function

map() function is a built-in function which returns a map object after applying function to each item upto a given iterables like list, tuples, etc.


syntax of map() function will be
map(function/expression, iterable)


for example:
def mul(n):
    return  n*n
num=(1,2,3,4)
res=map(mul,num)
print(list(res))


Output will be
[1,4,9,16]

 

map() function with lambda expression 
num=(1,2,3,4)
res=map(lambda x:x*x, num)
print(list(res))


output will be
[1,4,9,16]

 

adding two list using map and lambda
num1=[2,4,6]
num2=[3,5,7]
res=map(lambda x,y, x+y, num1,num2)
print(list(res))


output 
[5,9,13]

 

Map() function with a list of strings
li=[‘apple’,’bat’,’cat’,’dog’]
res=list(map(list,li))
print(res)


output:
[[‘a’,’p’,’p’,’l’,’e’],[‘b’,’a’,’t’],[‘c’,’a’,’t’],[‘d’,’o’,’g’]]

 

Map() Function for a tuple value
map_tuple_upper=map(to_upper_case(‘python’,’advanced java’, ‘dot net’))
output_Disp(map_tuple_upper)

 

Output:
PYTHON  ADVANCED JAVA  DOT NET

map() function usinga a lambda function using multiple arguments;
a1=[[1,2,3],[1.4,23.34,45.6]]
a2=[[45,65,76],[-2,-2.34,-5.6]]
map(lambda a1,a2:cmp(a1,a2).a1.a2)

 

output:
[-1,1]


Python Filter Function


Filter() method is used to test each element in a sequence with the help of a condition and checks each element in the sequence to be true or not.
syntax:
filter(function, sequence)
example:
def fun(var):
    vowles=[‘a’,’e’,’i’,’o’,’u’]
   if (var in vowels):
       return True
  else:
       return False

seq=[‘I’,’n’,’d’,’I’,’a’,’t’,’o’,’d’,’a’,’y’]
filtered=filter(fun,seq)
print(“The letter after filtering are:”)
  for s in fitered:
       print(s)

 

output:
i  i  a  o  a

map() function is used with lambda functions  to get separate list, tuple, or sets
sequence=[0,1,2,3,5,8,13]
res=filter(lambda  x:x%2,sequence)
print(list(res))
res = filter(lambda x: x%2==0, sequence)
print(list(res))


output:
[1,3,5,13]
[0,2,8]

 

Click here to enroll now for Python

 

Range function in Python


Python consists of different data types like list, tuple, etc., Python range() function is a built-in functions which is mainly used for looping statements.  It is used to store the sequence of numbers in a specified manner. It generates the sequence of number by taking starting parameter and ending parameter. The default values for range() function is 0 and it increment the value by 1 stops until the end value. 


Syntax of Range() function will be
range(start, stop, step)
Start: Start is an optional parameter, start value is used to specify the starting range of the sequence. By default it starts with zero if no starting value is specified.


Stop: Stop is an mandatory parameter. Compulsorily we have to specify the end value, it is mainly used to stop the sequence of numbers until a specified value.


Step: Step is also an optional parameter. It is mainly used to increment or decrement the sequence with a specific step values. If the step value is not specified than it will consider has one.
ex:
range(1,10)
it executes values from 1 to 9 only, here start will be 1 and end will be less than 10 because in range() function the end value will be excluded.
example:
for i in range(1,10):
   print( i,  end =’ ‘)
output:
1,2,3,4,5,6,7,8,9

 

for i in range(10):
   print(i, end =’ ‘)
output:
  1,2,3,4,5,6,7,8,9

for i in range(1,10,2):
   print(i,end=’ ‘)
output:
1,3,5,7,9

 

learn more of Python

 

Range function vs Xrange:
Python 3.x supports range() function, python 2.x supports xrange() function.
xrange() return values as object i.e is like generator object where as range() returns sequence of numbers as list.
xrange() methods take less memory because it returns as object but range() method takes more memory because it returns output values as a list of values.
Manipulation process or xrange object is not easy because xrange() methods returns an object but range() method is easy to manipulate because it returns list of values.

 

Related Blogs

what are python sequences and its operations