Python
    About Lesson

    Overview

    The map()filter() and reduce() functions bring a bit of functional programming to Python. All three of these are convenience functions that can be replaced with List comprehensions or loops but provide a more elegant and short-hand approach to some problems.

     

    map() function

    The map() function in Python is a built-in function used to apply a given function to each item of an iterable (like a list, tuple, etc.) and returns a new iterable with the results.

    The map() the function iterates through all items in the given iterable and executes the functions we passed as an argument on each of them.

     

    Syntax

    map(function, iterable(s))

     

    • function: The function to be applied to each item of the iterable.
    • iterable: An iterable like a list, tuple, etc., whose elements will be processed by the function.

     

    Example

    def starts_with_A(s):
    	return s[0] == "A"
    
    fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"]
    
    map_object = map(starts_with_A, fruit)
    print(list(map_object))

     

    Output:

    [True, False, False, True, False]

    As we can see, we ended up with a new list where the function starts_with_A() was evaluated for each of the elements in the list fruit. The results of this function were added to the list sequentially.

    The same can be done by the lambda function

    fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"]
    map_object = map(lambda s : s[0] == "A", fruit)
    print(list(map_object))

     

    Example 1: Square each number in a list

    # Define the function to square a number
    def square(x):
    return x * x

    # List of numbers
    numbers = [1, 2, 3, 4, 5]

    # Use map() to square each number in the list
    squared_numbers = map(square, numbers)

    # Convert the map object to a list
    squared_numbers_list = list(squared_numbers)

    print(squared_numbers_list)
    # Output: [1, 4, 9, 16, 25]

     

    Example 2: Convert a list of strings to uppercase

    # List of strings
    words = ['apple', 'banana', 'cherry']

    # Use map() to convert each string to uppercase
    uppercase_words = map(str.upper, words)

    # Convert the map object to a list
    uppercase_words_list = list(uppercase_words)

    print(uppercase_words_list)
    # Output: ['APPLE', 'BANANA', 'CHERRY']

     

    Example 3: Using lambda function with map()

    # List of numbers
    numbers = [1, 2, 3, 4, 5]

    # Use map() with lambda function to square each number in the list
    squared_numbers = map(lambda x: x * x, numbers)

    # Convert the map object to a list
    squared_numbers_list = list(squared_numbers)

    print(squared_numbers_list)
    # Output: [1, 4, 9, 16, 25]

     

    Example 4: Applying map() on multiple iterables

    # Lists of numbers
    numbers1 = [1, 2, 3]
    numbers2 = [4, 5, 6]

    # Use map() to add corresponding elements of two lists
    sums = map(lambda x, y: x + y, numbers1, numbers2)

    # Convert the map object to a list
    sums_list = list(sums)

    print(sums_list)
    # Output: [5, 7, 9]

     

    The map() function applies the specified function (either defined separately or using a lambda function) to each item of the iterable and returns an iterator with the results. You can then convert this iterator to a list or use it in other ways depending on your requirements.