Skip to content

Commit dbb3516

Browse files
committed
Added explanations for zip(), filter() and map()
1 parent 9b7a74b commit dbb3516

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

concepts/generators/about.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,120 @@ Now whenever `__next__()` is called on the `infinite_sequence` object, it will r
145145
[iterator]: https://docs.python.org/3.11/glossary.html#term-iterator
146146
[lazy iterator]: https://en.wikipedia.org/wiki/Lazy_evaluation
147147
[yield expression]: https://docs.python.org/3.11/reference/expressions.html#yield-expressions
148+
149+
150+
151+
## ZIP function
152+
153+
The zip() function in python is built in function. it pairs the two or more iteratbles and combines them one by one in into a single data structure, only done when needed (lazy evaluation)
154+
155+
Example:
156+
l1= ["AA","BB","CC"]
157+
l2=[1,2,3]
158+
l3= zip(l1, l2)
159+
print(list(l3))
160+
161+
output
162+
[('AA', 1), ('BB', 2), ('CC', 3)]
163+
164+
165+
## map function
166+
167+
the map() function in the Python takes 2 arguments that is : 1. A function, 2. list or any iterable
168+
then the map() function applies the function to each of the item in iterable
169+
Example:
170+
l1=[1,2,3]
171+
result = map(lambda x:x**2, l1)
172+
print(list (result) )
173+
174+
output:
175+
[1, 4, 9]
176+
177+
In short map() map() helps you apply a rule to each and every item in the list (or iterable) does by without using any extra memory
178+
179+
## filter
180+
181+
filter() function in python helps you to filter the items based on the specified condition.
182+
it filters only those items or iterables which are True
183+
Example: l2=[1,2,3]
184+
result = filter (lambda x:x%2==0 , l2)
185+
print(list (result) )
186+
187+
output:
188+
[2]
189+
In short filter() helps you pick only the items that match a rule
190+
191+
192+
193+
194+
195+
196+
zip() Function
197+
The zip() function in Python is a built-in that pairs elements from two or more iterables together. It combines them element-by-element into a single iterable — and does this lazily (only when needed).
198+
199+
Example:
200+
201+
python
202+
Copy
203+
Edit
204+
l1 = ["AA", "BB", "CC"]
205+
l2 = [1, 2, 3]
206+
l3 = zip(l1, l2)
207+
print(list(l3))
208+
Output:
209+
210+
css
211+
Copy
212+
Edit
213+
[('AA', 1), ('BB', 2), ('CC', 3)]
214+
🔹 In short: zip() lets you group related items together from different iterables.
215+
216+
map() Function
217+
The map() function takes:
218+
219+
A function
220+
221+
An iterable (like a list)
222+
223+
It applies the function to each item in the iterable — and returns a map object (like a generator) without creating a new list in memory.
224+
225+
Example:
226+
227+
python
228+
Copy
229+
Edit
230+
l1 = [1, 2, 3]
231+
result = map(lambda x: x**2, l1)
232+
print(list(result))
233+
Output:
234+
235+
csharp
236+
Copy
237+
Edit
238+
[1, 4, 9]
239+
🔹 In short: map() helps you apply a rule or transformation to every item in a list.
240+
241+
filter() Function
242+
The filter() function also takes:
243+
244+
A function that returns True or False
245+
246+
An iterable
247+
248+
It filters out only the items where the function returns True.
249+
250+
Example:
251+
252+
python
253+
Copy
254+
Edit
255+
l2 = [1, 2, 3]
256+
result = filter(lambda x: x % 2 == 0, l2)
257+
print(list(result))
258+
Output:
259+
260+
csharp
261+
Copy
262+
Edit
263+
[2]
264+
In short: filter() helps you pick only the items that match a rule

0 commit comments

Comments
 (0)