From 54eec70ca9001137fb9d80d442c3fd7d31f7b1ce Mon Sep 17 00:00:00 2001 From: Pranshu shah <97401387+shahpranshu27@users.noreply.github.com> Date: Sat, 25 May 2024 07:25:26 +0530 Subject: [PATCH] create map-function.md --- contrib/advanced-python/index.md | 1 + contrib/advanced-python/map-function.md | 54 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 contrib/advanced-python/map-function.md diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 880b393..e1ff216 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -5,3 +5,4 @@ - [Regular Expressions in Python](regular_expressions.md) - [JSON module](json-module.md) - [OOPs](OOPs.md) +- [Map Function](map-function.md) diff --git a/contrib/advanced-python/map-function.md b/contrib/advanced-python/map-function.md new file mode 100644 index 0000000..be035d0 --- /dev/null +++ b/contrib/advanced-python/map-function.md @@ -0,0 +1,54 @@ +The `map()` function in Python is a built-in function used for applying a given function to each item of an iterable (like a list, tuple, or dictionary) and returning a new iterable with the results. It's a powerful tool for transforming data without the need for explicit loops. Let's break down its syntax, explore examples, and discuss various use cases. + +### Syntax: + +```python +map(function, iterable1, iterable2, ...) +``` + +- `function`: The function to apply to each item in the iterables. +- `iterable1`, `iterable2`, ...: One or more iterable objects whose items will be passed as arguments to `function`. + +### Examples: + +#### Example 1: Doubling the values in a list + +```python +# Define the function +def double(x): + return x * 2 + +# Apply the function to each item in the list using map +original_list = [1, 2, 3, 4, 5] +doubled_list = list(map(double, original_list)) +print(doubled_list) # Output: [2, 4, 6, 8, 10] +``` + +#### Example 2: Converting temperatures from Celsius to Fahrenheit + +```python +# Define the function +def celsius_to_fahrenheit(celsius): + return (celsius * 9/5) + 32 + +# Apply the function to each Celsius temperature using map +celsius_temperatures = [0, 10, 20, 30, 40] +fahrenheit_temperatures = list(map(celsius_to_fahrenheit, celsius_temperatures)) +print(fahrenheit_temperatures) # Output: [32.0, 50.0, 68.0, 86.0, 104.0] +``` + +### Use Cases: + +1. **Data Transformation**: When you need to apply a function to each item of a collection and obtain the transformed values, `map()` is very handy. + +2. **Parallel Processing**: In some cases, `map()` can be utilized in parallel processing scenarios, especially when combined with `multiprocessing` or `concurrent.futures`. + +3. **Cleaning and Formatting Data**: It's often used in data processing pipelines for tasks like converting data types, normalizing values, or applying formatting functions. + +4. **Functional Programming**: In functional programming paradigms, `map()` is frequently used along with other functional constructs like `filter()` and `reduce()` for concise and expressive code. + +5. **Generating Multiple Outputs**: You can use `map()` to generate multiple outputs simultaneously by passing multiple iterables. The function will be applied to corresponding items in the iterables. + +6. **Lazy Evaluation**: In Python 3, `map()` returns an iterator rather than a list. This means it's memory efficient and can handle large datasets without loading everything into memory at once. + +Remember, while `map()` is powerful, it's essential to balance its use with readability and clarity. Sometimes, a simple loop might be more understandable than a `map()` call. \ No newline at end of file