0
0
Pythonprogramming~15 mins

Why lambda functions are used in Python - See It in Action

Choose your learning style9 modes available
Why lambda functions are used
๐Ÿ“– Scenario: Imagine you have a list of numbers and you want to quickly create a new list with each number doubled. You want to do this without writing a full function because it's a simple task.
๐ŸŽฏ Goal: You will learn how to use a lambda function to double numbers in a list using the map() function.
๐Ÿ“‹ What You'll Learn
Create a list called numbers with the values 1, 2, 3, 4, 5
Create a lambda function that doubles a number
Use the map() function with the lambda to double each number in numbers
Convert the result of map() to a list called doubled_numbers
Print the doubled_numbers list
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Lambda functions are used when you need a quick, small function for simple tasks like transforming data in lists.
๐Ÿ’ผ Career
Many jobs require processing data quickly and cleanly. Lambda functions help write concise code for data manipulation and functional programming.
Progress0 / 4 steps
1
Create the list of numbers
Create a list called numbers with these exact values: 1, 2, 3, 4, 5
Python
Need a hint?

Use square brackets [] to create a list and separate numbers with commas.

2
Create a lambda function to double a number
Create a lambda function called double that takes one argument x and returns x * 2
Python
Need a hint?

A lambda function is written as lambda arguments: expression.

3
Use map() with the lambda function
Use the map() function with the double lambda and the numbers list. Convert the result to a list called doubled_numbers
Python
Need a hint?

The map() function applies a function to each item in a list.

4
Print the doubled numbers
Print the list doubled_numbers to see the doubled values
Python
Need a hint?

Use print() to show the list on the screen.