We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Map Function in Python With Examples

Map Function in Python With Examples

Map Function in Python

map() function is done by iterable special datatypes and its values which returns a list of the values for the given expression or a function and the iterable to be a list, tuple, set,dict

syntax

map(function/expression of Lambda function, iterable(list, tuple,set,dict))

Example For Python map() Function:

def to_lower_case(string):
return str(string).lower()

It’ string is an argument of the to_lower_case function and which will return the lower case string value of the given input value of the object. Now the output is going to print using a map function of the iterator elements.

def Output_Display(str_value):
for an in str_value
print(a, end=' ')
print('')  # This is used to go for new line

The Above example to convert into String lower case we need a two functions to convert lower case the First function is to convert the string into the lower case and the Second function which is used to print the output of each character of lower case string

Python map() Function for a string value:

map_str_lower = map(to_lower_case, 'BESANT TECHNOLOGY')
print(type(map_str_lower))
Output_Display(map_str_lower)

Output:

<class 'map'>
“besant technology”

The Above example to convert into String lower case we need a map functions to convert lower case the First function is to convert the string into the lower case and the Second function which is used to print the output of each character of lower case string

Python map() Function for a tuple value:

map_tuple_lower = map(to_lower_case, (‘PYTHON’, 'JAVA', 'AWS'))
Output_Display(map_tuple_lower)

Output:

python java aws

The Above example to convert into tuple of value lower case we need a map functions to convert lower case the First function is to convert the string into the lower case and the Second function which is used to print the output of each element of lower case string

Python map() Function for a list value:

map_list_lower = map(to_lower_case, [‘PYTHON’, 'JAVA', 'AWS'])
Output_Display(map_list_lower)

Output:

python java aws

The Above example to convert into a list of value lower case we need a map functions to convert lower case the First function is to convert the string into the lower case and the Second function which is used to print the output of each element of lower case string. The output of a Python map() function can be done into a list, tuple, set and we can use the above example for this.

map_list_lower = map(to_lower_case, [‘PYTHON’, 'JAVA', 'AWS'])
print(list(map_list_lower))
map_tuple_lower = map(to_lower_case, [‘PYTHON’, 'JAVA', 'AWS'])
print(tuple(map_tuple_lower))
map_set_lower = map(to_lower_case, [‘PYTHON’, 'JAVA', 'AWS'])
print(set(map_set_lower))

Output:

['python', 'java', 'aws']
{'python', 'java', 'aws'}
('python', 'java', 'aws')

Python map() function using a lambda function:

Using lambda functions we can use python map() function and the below example you can be square the vaklue of iterable value of each element and return into a list of value.

x = [2, 4, 3, 1]
map_lambda= map(lambda a: a* a, x)
print(list (map_lambda))

Output:

[4,16,9,1]

Python map() function using a lambda function using multiple arguments:

The below example which compares of two list values and return either the values like(1,-1,0) as output based on greater, lesser, equal.

x1=[[1,2,3],[1,2,34,56,7]]
x2=[[12,2,23],[-1,-2,34,56,-67]]
map(lambda x,y:cmp(x,y),x1,x2)

Output:

 [-1, 1]

Python Map Function

 

 

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
Besant Technologies WhatsApp