In this lesson we want to learn How to Iterate with Python Lambda. You can use map() function to iterate over an iterable (such as a list, tuple or set) and apply lambda function to each item.
What is Lambda in Python ?
In Python,lambda is keyword that is used to create anonymous functions. An anonymous function is a function without name which can be useful when you need to pass a small piece of code as an argument to another function, or when you want to write a short piece of code to be executed later.
A lambda function is defined using the following syntax:
1 |
lambda arguments: expression |
where arguments are the inputs to the function and expression is single line of code that is executed when the function is called.
Lambda functions can be used wherever function is expected for example as an argument to the map(), filter() or reduce() functions. they can also be assigned to variable or passed as return value from another function.
For example, let’s say you have list of numbers and you want to square each number in the list:
1 2 3 |
numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(lambda x: x**2, numbers)) print(squared_numbers) |
Output:
1 |
[1, 4, 9, 16, 25] |
Note: The map() function returns a map object, which is an iterator, so it needs to be converted to a list (or another iterable type) to be stored or printed.
This is more complex example that demonstrates the use of lambda function with other built in functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# a list of tuples, each representing a person's name and age people = [("John", 32), ("Bob", 28), ("Ahmad", 40), ("Tom", 35)] # sort the list by age using a lambda function as the key sorted_people = sorted(people, key=lambda x: x[1]) print("Sorted by age:", sorted_people) # extract the names from the sorted list and convert to uppercase names = list(map(lambda x: x[0].upper(), sorted_people)) print("Uppercase names:", names) # use filter to select only people with age >= 35 selected = list(filter(lambda x: x[1] >= 35, sorted_people)) print("Selected:", selected) |
In this example, the sorted() function is used to sort the list of people by age, where the lambda function lambda x: x[1] is used as the key to extract the age from each tuple. The map() function is then used to extract the names from the sorted list and convert them to uppercase. Finally, the filter() function is used to select only people with age greater than or equal to 35.
Run the code and this will be the result
FAQs:
How do you iterate a lambda function in Python?
Lambda functions in Python are used for simple operations and are not directly designed for iteration. However, you can use lambda functions in combination with functions like map(), filter() and reduce() to iterate over collections or perform operations on iterable elements.
How do I run a Python code in lambda?
Lambda functions in Python are designed for short, simple operations, typically on a single line. You cannot directly execute multiple lines of code inside a lambda function. But you can execute Python code indirectly by invoking lambda functions in other functions or by using lambda functions in combination with built-in functions like map(), filter(), and reduce().
Can you use a for loop in a lambda function?
No, you cannot use a for loop directly inside a lambda function. Lambda functions in Python are limited to a single expression. If you need to perform iteration or looping operations, you should use regular functions or list comprehensions instead.
Can a lambda function be used to iterate through a list?
Lambda functions can be used with functions like map(), filter(), and reduce() to iterate through a list or perform operations on its elements. But lambda functions themselves cannot directly iterate over a list. Instead, you can use a regular function or a list comprehension for more complex iteration tasks.
Subscribe and Get Free Video Courses & Articles in your Email