Mastering Lambda Functions in Python
Lambda functions, also known as anonymous functions, are a concise way to create small, single-expression functions in Python. They are particularly useful for short operations that you don't want to define as full-fledged functions, often used with functions like map, filter, and sorted. This challenge will test your understanding of lambda functions and their practical application.
Problem Description
Your task is to create several lambda functions to perform specific operations. You will be given a description of each operation and must implement it using a lambda expression. The lambda functions should be concise and efficient, adhering to the single-expression rule. You'll be using these lambda functions in conjunction with built-in functions to process lists.
What needs to be achieved:
-
Create three distinct lambda functions:
square: Takes a number as input and returns its square.is_even: Takes a number as input and returnsTrueif the number is even,Falseotherwise.string_length: Takes a string as input and returns its length.
-
Apply these lambda functions using
mapandfilteron sample lists.
Key requirements:
- All functions must be implemented as lambda expressions.
- The code must be readable and well-formatted.
- The solutions must produce the expected output as demonstrated in the examples.
Expected behavior:
The code should correctly define the lambda functions and apply them to the provided lists, producing the desired results.
Edge cases to consider:
- Empty lists should be handled gracefully by
mapandfilter(they will simply return empty iterators). - The
is_evenfunction should correctly identify even numbers, including 0. - The
string_lengthfunction should handle empty strings correctly (returning 0).
Examples
Example 1:
Input: numbers = [1, 2, 3, 4, 5]
Output: squared_numbers = [1, 4, 9, 16, 25]
Explanation: The `square` lambda function is applied to each element of the `numbers` list using `map`, resulting in a new list containing the squares of the original numbers.
Example 2:
Input: numbers = [1, 2, 3, 4, 5, 6]
Output: even_numbers = [2, 4, 6]
Explanation: The `is_even` lambda function is applied to each element of the `numbers` list using `filter`, resulting in a new list containing only the even numbers.
Example 3:
Input: strings = ["apple", "banana", "kiwi", ""]
Output: string_lengths = [5, 6, 4, 0]
Explanation: The `string_length` lambda function is applied to each element of the `strings` list using `map`, resulting in a new list containing the lengths of the original strings.
Constraints
- The lambda functions must be single expressions.
- Input lists will contain numbers (integers) or strings.
- The code should be efficient and avoid unnecessary complexity.
- The solution should be compatible with Python 3.
Notes
- Remember that lambda functions are limited to a single expression. You cannot include statements like
if/elsedirectly within a lambda function; you'll need to use conditional expressions (ternary operators) instead. - Consider using the
mapandfilterfunctions to apply your lambda functions to lists. These functions are designed to work well with anonymous functions. - Think about how to handle edge cases like empty lists or empty strings.