Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

lambda, map, and filter in Python

Posted on September 12, 2023 by admin

Certainly! Let’s create a simple example to illustrate how to use lambda, map, and filter in Python.

Let’s say we have a list of numbers and we want to perform some operations on them using map and filter along with lambda functions.

# Example list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Using map and lambda to square each number in the list
squared_numbers = list(map(lambda x: x**2, numbers))
print("Squared numbers:", squared_numbers)

# Using filter and lambda to keep only even numbers in the list
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)

In this example:

  1. We start with a list of numbers from 1 to 10.
  2. We use the map function along with a lambda function to square each number in the list. The lambda function lambda x: x**2 takes an input x and returns its square.
  3. We use the filter function along with a lambda function to keep only the even numbers in the list. The lambda function lambda x: x % 2 == 0 checks if a number is even (i.e., its remainder when divided by 2 is 0).

When you run this code, you will get the following output:

Squared numbers: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Even numbers: [2, 4, 6, 8, 10]

This demonstrates how you can use lambda functions in combination with map and filter to perform operations on lists in Python.

Category: programming, python, Tutorials

Post navigation

← visualize time series data in a Django web application
10 common algorithms →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme