Map Filter Functions Python – Are you ready to supercharge your Python skills? Today, we’ll dive into the world of lambda expressions, map(), and filter() functions. These powerful tools will transform the way you manipulate data in Python. Let’s get started!
Lambda Expressions: Your Secret Weapon for Concise Code
First things first, let’s warm up with lambda expressions. These small, anonymous functions pack a punch when it comes to data manipulation tasks.
What’s a Lambda Expression?
A lambda expression creates a function object without using the standard def keyword. Here’s a simple example:
add_five = lambda x: x + 5
This lambda function takes an argument x
and returns x + 5
. It’s short, sweet, and to the point!
Capitalizing Words with Lambda
Want to capitalize a word? Lambda’s got you covered:
capitalize_word = lambda word: word.upper()
Now you can easily capitalize any word by calling capitalize_word("hello")
.
Map(): Transform Your Data with Ease
Next up, let’s explore the map() function. This handy tool applies a specified function to every element in an iterable.
How Does Map() Work?
The map() function takes two arguments: a function and an iterable. It then applies the function to each item in the iterable. Here’s an example:
names = ["alice", "bob", "CHARLIE", "dEborah"]
capitalized = list(map(lambda name: name.capitalize(), names))
print(capitalized)
# Output: ['Alice', 'Bob', 'Charlie', 'Deborah']
In this code, we use a lambda function with map() to capitalize each name in our list.
Discounting Prices with Map()
Let’s say you want to apply a 10% discount to a list of prices. Map() makes this a breeze:
prices = [25.99, 14.50, 8.75, 19.95]
discounted_prices = list(map(lambda price: price * 0.9, prices))
print(discounted_prices)
# Output: [23.391, 13.05, 7.875, 17.955]
Filter(): Find the Needle in Your Data Haystack
Last but not least, let’s tackle the filter() function. This powerful tool helps you extract specific items from an iterable based on a condition.
How Does Filter() Work?
Like map(), filter() takes a function and an iterable as arguments. It returns only the items for which the function evaluates to True. Check out this example:
products = ["Table", "Sofa", "Cushion", "Bookshelf", "Vase"]
four_letter_products = list(filter(lambda name: len(name) == 4, products))
print(four_letter_products)
# Output: ['Sofa', 'Vase']
Here, we use filter() with a lambda function to find products with names that are exactly four characters long.
Filtering Dictionary Items
You can even use filter() with dictionaries! Here’s how to find products under $90:
products = {'Table': 110, 'Sofa': 120, 'Chair': 45, 'Lamp': 70}
affordable_products = dict(filter(lambda item: item[1] < 90, products.items()))
print(affordable_products)
# Output: {'Chair': 45, 'Lamp': 70}
Wrapping Up: Your New Python Superpowers
Map Filter Functions Python – Congratulations! You’ve just leveled up your Python skills with lambda expressions, map(), and filter(). These powerful tools will help you write cleaner, more efficient code for data manipulation tasks.
Remember:
- Use lambda for quick, one-off functions
- Apply map() to transform entire iterables
- Utilize filter() to extract specific items based on conditions
Now go forth and code like a Python pro! For more advanced Python techniques, check out the official Python documentation.
Happy coding!
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.