Built with Tutify.io

    AIStudyLabs

    map, filter, and reduce

    map, filter, and reduce

    Introduction

    In this lesson, we'll delve into three powerful functions provided by Python for functional programming: map(), filter(), and reduce(). These functions are essential for performing transformations, filtering, and aggregations over iterables efficiently. We'll explore how these functions can be used to cleanly replace common loop patterns and give you insight into choosing the appropriate method for different scenarios.

    Using map()

    The map() function is used to apply a given function to each item of an iterable (like a list or tuple) and return a map object (an iterator).

    Syntax

    map(function, iterable)
    

    Example

    Let's double each number in a list using the map() function:

    numbers = [1, 2, 3, 4]
    double_function = lambda x: x * 2
    doubled_numbers = list(map(double_function, numbers))
    
    # Output:
    [2, 4, 6, 8]
    

    Using filter()

    The filter() function is used to construct an iterator from elements of an iterable for which a function returns true.

    Syntax

    filter(function, iterable)
    

    Example

    We'll filter out odd numbers from a list:

    numbers = [1, 2, 3, 4, 5]
    odd_filter = lambda x: x % 2 != 0
    odd_numbers = list(filter(odd_filter, numbers))
    
    # Output:
    [1, 3, 5]
    

    Using reduce()

    The reduce() function, which is available in the functools module, cumulatively applies a function to the items of an iterable to reduce it to a single cumulative value.

    Syntax

    from functools import reduce
    reduce(function, iterable[, initializer])
    

    Example

    Let's use reduce() to calculate the factorial of a number:

    from functools import reduce
    numbers = [1, 2, 3, 4, 5]
    factorial = reduce(lambda x, y: x * y, numbers)
    
    # Output:
    120
    

    Choosing Between map(), filter(), reduce(), and Other Constructs

    Example: Comparison

    Transforming a list using list comprehension:

    doubled_numbers = [x * 2 for x in numbers]
    

    Filtering using list comprehension:

    odd_numbers = [x for x in numbers if x % 2 != 0]
    

    Writing Small, Pure Functions

    To leverage map(), filter(), and reduce() effectively, write small, pure functions that are easy to test and debug. A pure function is one that has no side effects and returns the same output given the same input.

    Example

    def is_odd(n):
        return n % 2 != 0
    
    odd_numbers = list(filter(is_odd, numbers))
    
    # Output:
    [1, 3, 5]
    

    Summary / Key Takeaways

    In this lesson on map, filter, and reduce, you have learned:

    Python Programming – Complete Course with Practical Examples — Functional Programming Concepts in Python

    Getting Started with Python and the Course

    Python Syntax, Indentation, and Core Concepts

    Variables, Data Types, and Type Conversion

    Operators, Expressions, and Control Flow

    Loops and Iteration Patterns

    Working with Strings in Depth

    Collections: Lists, Tuples, Sets, and Dictionaries

    Functions and Program Structuring

    Functional Programming Concepts in Python

    Modules, Packages, and Code Organization

    Error Handling and Debugging Fundamentals

    File Handling and Data Formats

    Object-Oriented Programming in Python

    Advanced Python: Iterators, Generators, and Decorators

    Python Standard Library for Real-World Tasks

    Python for Automation and Scripting

    Testing and Quality Assurance in Python

    Tutorial Not Found