Search notes:

Python: map

map can be used to apply a function to each element in a list. It's probably the closest equivalent to the equally named Perl function.

Square numbers

The following snippet uses a lambda expression to square the numbers in a list:
#!/usr/bin/python

nums    = [4, 9, 5, 2, 8]

#
#   Apply a lambda expression on each element of nums
#   to calculate the square of the number and return
#   the number and its square in a tuple:
#
for squared in map(lambda num: (num, num*num), nums):
    print(' {:d}^2 = {:2d}'.format(squared[0], squared[1]))
Github repository about-Python, path: /builtin-functions/map/square-items.py

See also

The functionality of map can often (always) be achieved with a list comprehension.
Python: Built in functions
Using map to convert numpy arrays to torch tensors
Common first class function: map

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758196828, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/built-in-functions/map(65): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78