Cool things you can do with map in python

Photo by Marjan Blan | @marjanblan on Unsplash

What is a map() function?

The map object can take different types of functions. They are

  1. A normal function
  2. A lambda function
  3. A built-in function
  4. A filter function

The main purpose of using the map function is to perform operations on the iterables without manually having to iterate over them.

Let us look at some of the cool things we can do with map functions with different types of functions.

Map with normal functions

Square the numbers in the list using a normal function

map() with a custom function

Remove punctuations with the map function

Removing punctuations using the map() function

Map with lambda functions

The syntax for a lambda function is,

lambda arguments : expression

Square the numbers in the list using a lambda function

square numbers using map and lambda function
cube numbers using map and lambda function

Map function with two arguments

Map function with built-in functions

Change the data type of the values

map function with int datatype

Find the length of the string

map function with len

Using math functions with the map function

map function with ceil

Round the values using the map function

map function with round

Generate a list of numbers

map function with range

Get absolute value of the numbers

map function with abs

Map with filter

  1. The function will be a predicate or Boolean-valued function, a function that returns True or False according to the input data.
  2. Iterable will be any Python iterable.

Square of positive integers using map and filter

map with filter

Conclusion

Happy coding!

Python Developer | Django Developer | Python Enthusiast