DEV Community

Yasout141516
Yasout141516

Posted on

Map Function

MAPS

I have recently learned the Map function in python and I am amazed by its application . The map function essentially eliminates the use for a loop in a program. As a result the code comes out cleaner and the best part of it is the immutability it offers . This means that a new array is returned after it is done iterating on each element from the list .

A Function that implements a for loop

Image description

Although, the logic above is correct the code has turned out to be unnecessarily long and for more complex functions this code can be prone to bugs .

A Function that implements map()

Image description

The map function has not only reduced the size of the code but its readability is much easier now .

Top comments (1)

Collapse
 
vladzen13 profile image
Vladislav Zenin

Good for you) map is a first step in functional programming)

word[1:len(word)] and word[1:] work completely the same, but second is shorter and more readable

print([capitalize(name) for name in names]) - maximum readability i guess

map looks like a function, but it is not. it is callable and returns map objects, so it is a type.