✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 03 · Advanced Python Techniques

Transform. Filter.
Sort.

Turn raw data into useful results by changing values, keeping what matters, and ordering the outcome.

Learning goalUse map(), filter(), and sorted() with simple lambda functions, then choose the clearest approach.
01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Three operations, three different jobs.

map() transforms every item, filter() keeps only items that pass a condition, and sorted() creates an ordered list. In Python 3, map and filter return iterators, so list() often makes their results visible.

prices = [100, 200, 300]
discounted = list(map(lambda price: price * 0.9, prices))
print(discounted)

map() calls the lambda for every price. list() collects its iterator into [90.0, 180.0, 270.0]; prices stays unchanged.

[90.0, 180.0, 270.0]

filter() keeps original items whose condition is true. sorted() returns a new list, while list.sort() changes a list in place and returns None.

↳

Choose the clearest form: A comprehension is often easier to read for a small transformation or filter. map() and filter() are useful when you already have a function to pass.

Practical examples
scores = [45, 82, 67, 91, 38]
passed = list(filter(lambda score: score >= 60, scores))

products = [{"name": "Mouse", "price": 20}, {"name": "Keyboard", "price": 45}, {"name": "Monitor", "price": 180}]
ordered = sorted(products, key=lambda product: product["price"], reverse=True)

numbers = [1, 2, 3, 4, 5]
with_map = list(map(lambda n: n * 2, numbers))
with_comprehension = [n * 2 for n in numbers]

filter() returns the passing scores without changing them. sorted() can use reverse=True for descending order and is stable: equal keys keep their original order.

Filter, then transform
available = filter(lambda product: product["price"] >= 50, products)
discounted = map(lambda product: product["price"] * 0.9, available)

The filter decides which products continue. map() then changes only their prices.

Sort a new list
ordered = sorted(products, key=lambda product: product["price"])

sorted() leaves products unchanged. Use .sort() only when changing that same list is intended.

Follow the data pipeline

  1. 1. Start with original dataBegin with a small collection of values or dictionaries.
  2. 2. filter() selectsIts function returns True for elements that stay.
  3. 3. map() transformsIts function returns the new value for every remaining element.
  4. 4. sorted() ordersIt returns a new list in ascending order unless reverse=True is used.

Common mistakes

  • Expecting map() to be a listWrap map() or filter() in list() when you need a list to print or reuse.
  • Mixing selection and transformationfilter() keeps original elements; map() creates transformed values.
  • Expecting sorted() to mutatesorted() returns a new list. list.sort() mutates the list and returns None.
  • Making one long chainName intermediate results when nesting map(), filter(), and lambda makes code hard to read.

Python Playground

Loading Python runtime…

Change the price threshold or discount rate, then observe which products remain and how their sorted discounted prices change.

data_pipeline.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

filter() first keeps numbers greater than 5. map() then doubles only those selected numbers.

numbers = [2, 5, 8, 11]
selected = filter(lambda n: n > 5, numbers)
result = list(map(lambda n: n * 2, selected))
print(result)
Choose the output

Practice independently

Mini Challenge — Online shop data

Build the pipeline one step at a time: available products, discounted prices, then sorted results.

Your goalFilter in-stock products, calculate a 10% discount with map(), sort the prices, and print the result. Bonus: keep each product name paired with its discounted price.

online_shop_challenge.py

Optional hints
  • Use filter() first: the condition should inspect product["stock"].
  • map() can return a discounted price. For the bonus, return a dictionary or tuple that keeps the name with that price.
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.