• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Filter / Getting Started With Map, Filter, and Reduce in Python

Getting Started With Map, Filter, and Reduce in Python

March 9, 2012 by cmdlinetips



Python has a couple of nice built-in function in the flavor of functional programming paradigm. These built-in functions like map, filter, and apply combine very well with lambda functions. Map, filer, and reduce help you create, work with and tame lists in Python.

For example sure, at some point you will have to deal with each and every element of the list. A naive approach to do something with each element of a list is to use a for loop. In general, for loops are frowned upon and you should always look for ways to write code with one loop lesser. Thats where these functional constructs in Python come in extremely handy.

Map Function in Python

In the simplest form, the built-in function map takes in a function and a list as input and applies the function to each element of the list. As you can immediately see that, in this case map can replace a for loop. Let us consider an example of using the map function. Let us say we have a list of numbers like

>>>myList = range(10)
>>>myList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For each element in the list, if we want to compute a function f(x) (say 3*x^2)), where x is a list element and input to the function, the (one) pythonic way of getting this done is to avoid loops and write

>>>map(lambda x: 3*x**2, myList)
[0, 3, 12, 27, 48, 75, 108, 147, 192, 243]

Note that one can use any complex function that does something with the list elements and return a list of the same size.

Filter Function in Python

Filter function works very similar to the map function. Instead of just applying the function on all the elements in the list, filter function filters the elements based on the condition used in the lambda function.For example, if we want to find the even numbers in the list “myList”, the filter function will come handy.

>>>filter(lambda x: x%2==0, myList)
[0, 2, 4, 6, 8]

Reduce Function in Python

Reduce function in Python will come handy when you want to operate on a list and reduce to a single value. A simple example using the reduce function will be to use to find the sum of all the elements of a list, like

>>>>reduce(lambda x, y: x+y, myList)
45

Basically, reduce function adds the first two elements and adds the result to next element in the list till. Reduce does this till it is reduced to one value.

If you think, the map, filter, and reduce functions are cool, wait till you learn about List Comprehensions in Python.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailMastering Lists in Python Using List Comprehensions Default ThumbnailHow to Compute Executing Time in Python? Default ThumbnailHow to Use Lambda Functions in Python? Default ThumbnailGetting Started With Python Dictionaries: Python Tips

Filed Under: Filter, Lambda Function, Map, Python, Python Functional Programming, Python Tips, Reduce Tagged With: Python Filter, Python Functional Programming, Python Map, Python Reduce, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version