How to use map, filter and reduce?
When working on Python programming you inevitably come across situations where you have to do some data manipulation. In most cases, you use control statements to get the desired result, but these control statements can quickly become a bit messy and large. Functional techniques can help you write more declarative code that is easier to understand at a glance, refactor, and test. Most of the times it can be much easier to use the map , filter or reduce methods.
The rule of thumb you use to determine which method you should use is as follows:
- If you already have a list of values and you want to do the exact same operation on each of the elements in the array and return the same amount of items in the list, in these type of situations it is better to use the map method.
- If you already have list of values but you only want to have items in the array that match certain criteria, in these type of situations it is better to use the filter method.
- If you already have list of values, but you want to use the values in that list to create something completely new, in these type of situations it is better to use the reduce method.
Map operation takes a mapping function and a vector of data as arguments and returns a new vector, which is the result of applying the mapping function on each element of the vector independently. The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) and so on.
- function_to_apply - map() passes each item of the iterable to this function.
- list_of_inputs - iterable which is to be mapped
Most of the times map function use lambdas.
The filter function operates on a list and returns a subset of that list after applying the filtering rule.
The reduce function will transform a given list into a single value by applying a given function continuously to all the elements. It basically keeps operating on pairs of elements until there are no more elements left.
The following example shows how to find the product of given numbers.
outputUsing Reduce method:
- Python Interview Questions FAQ - 1
- Python Interview Questions FAQ - 2
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- Global and Local Variables in Python
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- How to avoid having class data shared among instances in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- Is monkey patching considered good programming practice?