Open In App

Python: Iterating With Python Lambda

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python.

Syntax:

lambda variable : expression

Where,

  1. variable is used in the expression
  2. expression can be an mathematical expression

Example 1:

 In the below code, We make for loop to iterate over a list of numbers and find the square of each number and save it in the list. And then, print a list of square numbers.

Python3




# Iterating With Python Lambdas
  
# list of numbers
l1 = [4, 2, 13, 21, 5]
  
l2 = []
  
# run for loop to iterate over list
for i in l1:
      
    # lambda function to make square 
    # of number
    temp=lambda i:i**2
  
    # save in list2
    l2.append(temp(i))
  
# print list
print(l2)


Output:

[16, 4, 169, 441, 25]

Example 2:

We first iterate over the list using lambda and then find the square of each number. Here map function is used to iterate over list 1. And it passes each number in a single iterate. We then save it to a list using the list function. 

Python3




# Iterating With Python Lambdas
  
# list of numbers
l1 = [4, 2, 13, 21, 5]
  
# list of square of numbers
# lambda function is used to iterate 
# over list l1
l2 = list(map(lambda v: v ** 2, l1))
  
# print list
print(l2)


Output:

[16, 4, 169, 441, 25]

Example 3:

In the below code, we use map, filter, and lambda functions. We first find odd numbers from the list using filter and lambda functions. Then, we do to the square of it using map and lambda functions as we did in example 2.

Python3




# Iterating With Python Lambdas
  
# list of numbers
l1 = [4, 2, 13, 21, 5]
  
# list of square of odd numbers
# lambda function is used to iterate over list l1
# filter is used to find odd numbers
l2 = list(map(lambda v: v ** 2, filter(lambda u: u % 2, l1)))
  
# print list
print(l2)


Output:

[169, 441, 25]


Previous Article
Next Article

Similar Reads

Python | Delete items from dictionary while iterating
A dictionary in Python is an ordered collection of data values. Unlike other Data Types that hold only a single value as an element, a dictionary holds the key: value pairs. Dictionary keys must be unique and must be of an immutable data type such as a: string, integer or tuple. Note: In Python 2 dictionary keys were unordered. As of Python 3, they
3 min read
Python | Iterating two lists at once
Sometimes, while working with Python list, we can have a problem in which we have to iterate over two list elements. Iterating one after another is an option, but it's more cumbersome and a one-two liner is always recommended over that. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "+" operator The combina
5 min read
Why is iterating over a dictionary slow in Python?
In this article, we are going to discuss why is iterating over a dict so slow in Python? Before coming to any conclusion lets have a look at the performance difference between NumPy arrays and dictionaries in python: Python Code # import modules import numpy as np import sys # compute numpy performance def np_performance(): array = np.empty(1000000
3 min read
Python - Iterating through a range of dates
In this article, we will discuss how to iterate DateTime through a range of dates. Using loop and timedelta to Iterate through a range of dates Timedelta is used to get the dates and loop is to iterate the date from the start date to end date Syntax: delta = datetime.timedelta(days=1) Example: Python code to display the dates from 2021 - Feb 1st to
2 min read
Iterating List of Python Dictionaries
Iteration of the list of dictionaries is a very common practice that every developer performs while encountering with dictionary data. In this article, we will explore how to iterate through a list of dictionaries. Iterating List Of Dictionaries in PythonBelow are some of the ways by which we can iterate list of dictionaries in Python: Using a simp
3 min read
MoviePy – Iterating frames of Video File Clip
In this article we will see how we can iterate frames of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. A video is basically combination of lots of frame for each time there is a specific frame, in order to get the frame at given time we use get_frame method.
2 min read
Iterating over rows and columns in Pandas DataFrame
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe. In this article, we are using "nba.c
6 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Lambda and filter in Python Examples
Prerequisite : Lambda in Python Given a list of numbers, find all numbers divisible by 13. Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221] We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is witho
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Article Tags :
Practice Tags :