Skip to content

Commit a150877

Browse files
committed
feat: add code demonstrating usage of map, filter, and reduce functions in Python
1 parent dbf5e63 commit a150877

2 files changed

Lines changed: 106 additions & 5 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# map(), filter(), and reduce() in Python
2+
3+
4+
# ---------------------------
5+
# map()
6+
# ---------------------------
7+
# The map() function applies a given function to every element in an iterable
8+
# and returns a map object containing the transformed values.
9+
10+
numbers_list = [1, 2, 3, 4, 5]
11+
12+
13+
# Function to calculate the square of a number
14+
def calculate_square(number):
15+
return number * number
16+
17+
18+
# Convert the map object into a list to display all results
19+
squared_numbers_list = list(map(calculate_square, numbers_list))
20+
print("Squared numbers:", squared_numbers_list)
21+
22+
23+
# If we do not convert it to a list, map() returns an iterator.
24+
# We can access the values by iterating through it.
25+
26+
squared_numbers_iterator = map(calculate_square, numbers_list)
27+
28+
print("Squared numbers using iteration:")
29+
for squared_number in squared_numbers_iterator:
30+
print(squared_number)
31+
32+
33+
# Using map() with a lambda function
34+
numbers_multiplied_by_two = list(map(lambda number: number * 2, numbers_list))
35+
print("Numbers multiplied by 2:", numbers_multiplied_by_two)
36+
37+
38+
# ---------------------------
39+
# filter()
40+
# ---------------------------
41+
# The filter() function selects elements from an iterable
42+
# that satisfy a specific condition.
43+
44+
numbers_list = [3, 7, 10, 12, 4, 15]
45+
46+
47+
# Function to check whether a number is greater than 9
48+
def is_greater_than_nine(number):
49+
return number > 9
50+
51+
52+
# Apply filter() to get numbers greater than 9
53+
numbers_greater_than_nine = list(filter(is_greater_than_nine, numbers_list))
54+
print("Numbers greater than 9:", numbers_greater_than_nine)
55+
56+
57+
# Using filter() with a lambda function
58+
numbers_greater_than_two = list(filter(lambda number: number > 2, numbers_list))
59+
print("Numbers greater than 2:", numbers_greater_than_two)
60+
61+
62+
# ---------------------------
63+
# reduce()
64+
# ---------------------------
65+
# The reduce() function repeatedly applies a function to elements
66+
# of an iterable until a single result remains.
67+
# reduce() must be imported from the functools module.
68+
69+
from functools import reduce
70+
71+
numbers_list = [1, 2, 3, 4, 5, 6]
72+
73+
74+
# Calculate the sum of numbers using a normal loop
75+
total_sum = 0
76+
for number in numbers_list:
77+
total_sum = total_sum + number
78+
79+
print("Sum calculated using a loop:", total_sum)
80+
81+
82+
# Function used by reduce() to add two numbers
83+
def add_numbers(first_number, second_number):
84+
return first_number + second_number
85+
86+
87+
# Calculate the sum using reduce()
88+
sum_using_reduce = reduce(add_numbers, numbers_list)
89+
90+
91+
# Explanation of how reduce() works:
92+
# numbers_list = [1, 2, 3, 4, 5, 6]
93+
# Step 1: 1 + 2 = 3
94+
# Step 2: 3 + 3 = 6
95+
# Step 3: 6 + 4 = 10
96+
# Step 4: 10 + 5 = 15
97+
# Step 5: 15 + 6 = 21
98+
99+
print("Sum calculated using reduce():", sum_using_reduce)

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ The repository includes examples and practice code for various Python concepts,
1919
- Comments and Escape Sequences
2020
- String Operations and Slicing
2121
- Conditional Statements
22-
- Control Flow and Loops
22+
- Loops and Control Flow
2323
- Functions and Lambda Functions
2424
- Recursive Functions
25-
- Python Modules (internal, external via pip, and custom-built)
25+
- Modules (Built-in, External using pip, and Custom Modules)
2626
- Variable Scope and Docstrings
27-
- list, touple sets, dictionary
28-
- OOPS(Class, Object, Inheritance, Composition, Polymorphism, Abstraction, Encapsulation, Method Resolution Order)
29-
- dunder methods
27+
- Lists, Tuples, Sets, and Dictionaries
28+
- Object-Oriented Programming (Classes, Objects, Inheritance, Composition, Polymorphism, Abstraction, Encapsulation, Method Resolution Order)
29+
- Dunder (Magic) Methods
30+
- Exception Handling
31+
- Map, Filter, and Reduce
3032
- Basic Python Programs and Exercises
3133

3234
More topics will be added as I continue learning and practicing Python.

0 commit comments

Comments
 (0)