• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Lambda Function in Python
Next →
← Prev

Lambda Function in Python

By Dinesh Thakur

In this tutorial, You can read about the anonymous function, also known as lambda functions. You can understand what they are, how to use them, and their syntax (with examples).

We’ll be covering the following topics in this tutorial:

  • What is lambda function in Python?
  • Syntax of Lambda Function in python
  • How do you use lambda function in Python?
  • Difference between a normal def defined function and lambda function.
  • The following are the characteristics of Python lambda functions:
  • Use of Lambda Function in python

What is lambda function in Python?

A Lambda Function in Python programming is an anonymous single-line function defined without a name. Although the def keyword in Python describes normal functions, the lambda keyword defines anonymous functions. A lambda function may contain any number of arguments but can contain a single expression. These are beneficial if we have small tasks with fewer coding.

Syntax of Lambda Function in python

Syntax: lambda argument(s): expression

Lambda functions consist of three parts:

• Keyword
• Bound variable/argument, and
• Body or expression

The keyword is compulsory and must be a lambda, while the arguments and the body will vary depending on the circumstances.

How do you use lambda function in Python?

As described above, A Python lambda function may only provide a single expression with any number of arguments. The lambda operator has no statements and returns a function object to which we may add some variable.

For example:

# Program to show the use of lambda functions
cube = lambda y: y*y*y
print(cube(5))

Output:

125

In the above program, lambda y: y*y*y is the lambda function. Here y is the argument and y*y*y is the expression that gets evaluated and returned.

There is no name for this function. It returns a function object allocated to the identifier cube. We may term it a normal function. The statement

cube = lambda y: y*y*y

is nearly the same as:

def cube(y): 
  return y*y*y 

Difference between a normal def defined function and lambda function.

• def is a keyword not returning anything except in the local namespace generating a 'name.' Lambda is a keyword that returns a function object and does not create a 'name' in the local namespace.⠀
• A 'def' function can contain any python code. An expression function defined with lambda must be evaluated, and thus statements like print, import, raise cannot be included.
• The development of a lambda function is marginally faster than def. The distinction is that a name entry is generated in the local table. The resulting function has the same speed of performance.

The following are the characteristics of Python lambda functions:

• There is any number of arguments for a lambda function, but they contain just one expression. An expression is a piece of code that can return any value, and the lambda function implements that.
• The functions Lambda may use to return function objects.
• The lambda functions are syntactically restricted to only a single expression.

Use of Lambda Function in python

The Lambda functions in Python use along with built-in functions like filter(), map(), reduce(), etc.

lambda() Function with filter()

The filter() function in Python constructs an iterator from an iterable element for which a function returns true. The syntax of filter() method is: filter(function, iterable).

# Python code to illustrate 
# filter() with lambda() 
list = [5, 7, 97, 77, 23, 73, 61] 
filter_list = list(filter(lambda x: (x>50) , list)) 
   print(filter_list) 

Output:

[97, 77, 73, 61]

lambda() Function with map()

In python, the map() function is another built-in function that takes an function object and a list. The function is called using a lambda function, returns a list, and a new list containing all the modified lambda items returned for each object. The map function syntax is as follows:

# Python code to illustrate 
# map() with lambda() 
# to get double of a list. 
cube = [1, 2, 3, 4, 5, 6, 7] 
cubed_list = list(map(lambda x: x*x*x, cube)) 
   print(cubed_list) 

Output:

[1, 8, 27, 64, 125, 216, 343]

lambda() Function with reduce()

A list is taken as an argument by the reduce() function in Python. It behaves differently from map() and filter(). The reduce() function is iterable and a new reduced result is returned. The procedure is replicated over the pairs of the iterable.

# Python code to illustrate 
# reduce() with lambda() 
from functools import reduce
list = [1, 2, 3, 4, 5, 6, 7] 
multiply = reduce((lambda x, y: x * y), list) 
   print (multiply) 

Output:

5040

You’ll also like:

  1. map() Function in Python
  2. Switch Case in Python
  3. Linked list in Python
  4. Python String title() Method
  5. What is Python? | Introduction to Python Programming
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.