InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python map examples

    on Feb 2, 2016

    Python map is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that map applies to any iterable (e.g. list). We’ll use list for the purpose of this tutorial.

    Usage: map(function, iterable, ...)
    The function should take one or more arguments (depending upon how many iterables are passed) and should return new value.

    Here are some examples.

    map example with one list using function

    Given a list of numbers get a new list with items squared using function.

    def square(x):
      return x*x
    a = [1,2,4,10]
    b = map(square, a)
    print b
    [1, 4, 16, 100]
    
    Env: Python 2.7.18

    map example with one list using lambda

    Given a list of numbers get a new list with items squared using lambda.

    a = [1,2,4,10]
    b = map(lambda  x: x*x, a)
    print b
    [1, 4, 16, 100]
    
    Env: Python 2.7.18

    map example with two lists

    Given two lists of numbers get a new list where items are sum of items from these two lists.

    a = [1,2,4,10]
    b = [2,2,3,3]
    new = map(lambda  x, y: x+y, a, b)
    print new
    [3, 4, 7, 13]
    
    Env: Python 2.7.18

    Additional notes

    Note that if you are working on large data then it would be more memory efficient to use itertools imap which calls map function lazily.

    Suggested posts:

    1. Python itertools ifilter examples
    2. Javascript – iterate over function arguments
    3. Install ffmpeg on Ubuntu Linux
    4. Python/Perl/Unix one liners
    5. Python range examples
    6. Python for loop examples
    7. AngularJS ng-if – conditional dom tree
    8. Python list/array – remove an item
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python array, Python built-in functions, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress