oter

“Map” and “Filter” process sequences efficiently from "summary" of Fluent Python by Luciano Ramalho

The "map" function applies a function to each item in an iterable and returns an iterator that yields the results. By default, map creates a one-to-one mapping between the input iterable and the output iterable. However, you can pass multiple input iterables to map if the function takes multiple arguments. In that case, the mapping function must have as many arguments as there are input iterables. If you pass None as the first argument to map, the function returns tuples containing the items from the input iterables. This behavior is useful for combining data sources that have a natural one-to-one correspondence, such as pairs of coordinates or records from two related database tables. The "filter" function applies a function to each item in an iterable, yielding items for which the function returns True. Like map, filter returns an iterator. The function passed as the first argument to filter should return a boolean value. If the function is None, only items that are equivalent to True are returned. The filter function can be used to remove unwanted elements from a sequence, generating a subsequence of items that satisfy a specific condition. By combining filter with lambda functions, you can quickly create simple data processing pipelines that extract, transform, and filter items from a sequence. Both map and filter are lazy, meaning they only produce items as requested. This laziness can result in efficient memory usage when applied to large sequences. However, map and filter return iterators, so if you need to store the results in a list, you can pass the iterator to the list constructor to consume all the items and convert them into a list. The use of map and filter is idiomatic in Python and can lead to more expressive and concise code when working with sequences. By leveraging the power of these functions, you can process data efficiently and elegantly, transforming and filtering sequences with ease.
    oter

    Fluent Python

    Luciano Ramalho

    Open in app
    Now you can listen to your microbooks on-the-go. Download the Oter App on your mobile device and continue making progress towards your goals, no matter where you are.