Skip to main content
Background Image
  1. Posts/

Map Sort Filter Functions in Python

·2 mins·
Yalchin Mammadli
Author
Yalchin Mammadli
Experienced Python developer with 6+ years of expertise in Django, Flask, and FastAPI, specializing in high-performance web applications. Skilled in Node.js/TypeScript (Express), Docker, CI/CD, and TDD with 95% test coverage. Proven track record across ERP, CRM, e-commerce, blockchain, and AI projects. Focused on continuous growth and delivering impactful, scalable solutions.

This tutorial will help you to see real example of using map, sort, and filter functions along with lambda functions in Python.

Map Function Example

Python’s map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.

target_list = [1, 2, 3, 4, 5, 6]
func = lambda element: element ** 2
print(list(map(func, target_list)))

Sort Function Example

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

users = [("Yalchin", "age", 20), ("Coder", "age", 19)]
func = lambda user: user[2]
users.sort(key=func,reverse=True)
print(users)

Filter Function Example

The filter() method constructs an iterator from elements of an iterable for which a function returns true. In simple words, filter() method filters the given iterable with the help of a function that tests each element in the iterable to be true or not.

ages = [18, 33, 3, 10, 9, 23]
func = lambda age: age > 18
x = filter(func, ages)
print(list(x))

Related

Python Decorators With Function and Decorator Parameters
·1 min
How to Setup Mailgun in Django
·3 mins
Accent and Case Insensitive Search and Minimum Distance Calculator in Pymongo
·4 mins
Implementing Database Logs or History
·7 mins
How to Setup Github Ssh Key
·2 mins
How to Deploy Nodejs App
·3 mins