Maps and Filters in python

A cheerful cartoon monkey sorts bananas from a mixed fruit box into another box on a conveyor belt, with “Filtering Lists” text.

When working with lists, it’s common to want to apply the same action to each item, or to select only certain items. Python provides two handy tools for this: map() and filter(). These functions help you write shorter, clearer code compared to writing full loops every time.

The map() Function

The map() function applies a given function to every item in a list (or any iterable). This is great when you want to transform data in one step.

Here, lambda x: x**2 is a small function that squares each number. map() applies it to every item in numbers and gives back the results.

Another example with text:

Why the list()?

map() doesn’t actually create a list — it creates a map object, which is an iterator. That means it knows how to give you items one by one, but you can only loop through it once. Wrapping it in list() turns the result into a list you can print, reuse, and work with normally.

Things to try

  • Create a list of numbers and use map() to double each one.
  • Make a list of names and use map() with str.lower to turn them all lowercase.

The filter() Function

The filter() function chooses items from a list based on a condition. Only items where the condition is True are kept.

Here, the function checks if each number is even (x % 2 == 0). Only the even numbers are returned.

Another example with words:

Why the list()?

Just like map(), filter() gives you a filter object (another iterator). Wrapping it in list() makes the results visible and reusable. Without list(), printing it would just show something like <filter object at 0x...>.

Things to try

  • Create a list of numbers and filter out only the numbers greater than 50.
  • Make a list of words and filter to keep only those starting with the letter “a”.

Combining map() and filter()

You can use these functions together to process and clean data in a single readable chain.

This code finds all the even numbers, then maps a function to square them. Instead of writing a longer loop, you have one short line.

Things to try

  • Create a list of numbers 1–20. Use filter() to select only odd numbers, then map() to triple them.
  • Make a list of names, filter those longer than four letters, then convert them to uppercase.

Comparison with List Comprehensions

Python also has list comprehensions, which often achieve the same result as map() and filter() but with a slightly different style. Some developers find them easier to read, while others prefer map()/filter() for clarity.

Here’s the even-squares example again:

Using map() and filter():

Using a list comprehension:

Both produce the same result — it’s just a matter of style and what feels clearer to you.

Things to try

  • Rewrite a map() example as a list comprehension.
  • Rewrite a filter() example as a list comprehension.

Wrapping Up

  • map() transforms every item in a list using a function.
  • filter() keeps only the items that meet a condition.
  • Both return iterators, so you’ll usually wrap them in list() to see and reuse the results.
  • They can be combined for powerful data transformations.
  • List comprehensions often provide an alternative that many find even more readable.

Main Topic

Introducing Python Lists

A cartoon frog smiles while writing “Python Lists” with items flies, worms, insects on a chalkboard in a classroom setting.

This is a Python series introducing lists, their importance, and everyday uses. Light-hearted explanations make coding concepts simple, clear, and fun.

Other Tutorials in this Topic

A person’s hands holding pen and eraser above a notepad on a desk, crossing out “bread” from a shopping list.

Beginning Python Lists

A practical Python tutorial introducing lists, showing how to access, add, modify, and remove items with clear…

A cheerful goose sorts colourful books by size on a shelf, with bold text “Sorting Python Lists” against a pale magenta background.

Sorting Python Lists

A practical Python tutorial explaining how to sort lists in ascending and descending order, comparing `sort()` with…

A smiling blacksmith hammers words into metal blocks on an anvil, labelled “cat,” “7,” and “apple,” symbolising immutable Python tuples.

Tuples in Python

A Python tutorial explaining tuples: how to create them, their immutability, practical uses, adding to lists, and…

A person in an orange shirt works along a conveyor belt with boxes, apple, and envelope under text “Iterating Lists” on pale magenta.

Iterating Lists in python

A clear Python tutorial explaining how to loop through lists with for, while, and enumerate, introducing iterators…