Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Python map() Function - DesignLinux

designlinux 0 Comments

map() is a built-in function in Python that applies a function to all elements in a given iterable. It allows you to write simple and clean code without using loops.

Python map() Function #

The map() function takes the following form:

map(function, iterable, ...)

It accepts two mandatory arguments:

  • function – Function that is called for every element of iterable.
  • iterable – One or more objects that support iteration. Most of the built-in objects in Python, like lists, dictionaries, and tuples are iterables.

In Python 3, map() returns a map object with a size equal to the passed iterable object. In python 2 the function returns a list.

Let’s take a look at an example to explain better how the map() function works. Say we have a list of strings, and we would like to convert each element in the list into uppercase.

One way to do this is to use the traditional for loop:

directions = ["north", "east", "south", "west"]
directions_upper = []

for direction in directions:
    d = direction.upper()
    directions_upper.append(d)

print(directions_upper)
['NORTH', 'EAST', 'SOUTH', 'WEST']

With map() function, the code will be much simpler and more flexible.

def to_upper_case(s):
    return s.upper()

directions = ["north", "east", "south", "west"]

directions_upper = map(to_upper_case, directions)

print(list(directions_upper))

We’re using the list() function to convert the returned map object into a list:

['NORTH', 'EAST', 'SOUTH', 'WEST']

If the callback function is simple, the more Pythonic way is to use a lambda function:

directions = ["north", "east", "south", "west"]

directions_upper = map(lambda s: s.upper(), directions)

print(list(directions_upper))
A lambda function is a small anonymous function.

Here is another example showing how to create a list of square numbers from 1 to 10:

squares = map(lambda n: n*n , range(1, 11))
print(list(squares))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The range() function generates a sequence of integers.

Using map() with Multiple Iterables #

You can pass as many iterables as you want to the map() function. The number of required input arguments that the callback function accepts must be the same as the number of the iterables.

The following example shows how to perform element-wise multiplication on two lists:

def multiply(x, y):
  return x * y

a = [1, 4, 6]
b = [2, 3, 5]

result = map(multiply, a, b)

print(list(result))
[2, 12, 30]

The same code, but using lambda function will look this way:

a = [1, 4, 6]
b = [2, 3, 5]

result = map(lambda x, y: x*y, a, b)

print(list(result))

When multiple iterables are provided, the size of the returned object is equal to the shortest iterable.

Let’s see an example when the iterables are not of the same length:

a = [1, 4, 6]
b = [2, 3, 5, 7, 8]

result = map(lambda x, y: x*y, a, b)

print(list(result))

The excess elements (7 and 8) are ignored:

[2, 12, 30]

Conclusion #

The Python’s map() function takes an iterable object, along with a function and applies that function to each element in the iterable.

If you have any questions or feedback, feel free to leave a comment.

python

Related

Tags: python

How to Install Vagrant on Debian 10

Prev Post

How to Install Apache Maven on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 792
  • 604,754

DesignLinux.com © All rights reserved

Go to mobile version