This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lis is an iterable | |
lis = [1,2,3] | |
#lis_iter is an iterator | |
lis_iter = iter(lis) | |
for i in range(len(lis)): | |
print(next(iterator)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lis is an iterable | |
lis = [1,2,3] | |
#lis_iter is an iterator | |
lis_iter = iter(lis) | |
#next() will return consecutive values untill the end | |
for i in range(len(lis)): | |
print(next(iterator)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getrange(n): | |
```generator function``` | |
i = 0 | |
while i < n: | |
yield i | |
i += 1 | |
n = 3 | |
# calling generator function | |
a = getrange(n) # a is an iterator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lambda function | |
get_double = lambda x: x * 2 | |
print(get_double(5)) | |
# Output | |
>> 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = [1, 2] | |
list_b = [10, 20] | |
def add(a,b): | |
return a+b | |
m1 = map(add,list_a,list_b) #using normal function | |
m2 = map(lambda x, y: x + y, list_a, list_b) #using lambda function | |
>> print(list(m1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = list(range(5)) | |
def is_greater(n): | |
if(n>2): | |
return True | |
else: | |
return False | |
f1 = filter(lambda x : x>2, list_a) #lambda function | |
f2 = filter(is_greater, list_a) #normal function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools #reduce() is in functools package | |
lis = [1, 3, 5, 6, 2] | |
def get_greater(a,b): | |
if(a>b): | |
return a | |
else: | |
return b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#example_1 | |
numbers = [1, 2, 3, 4] | |
squares = [n**2 for n in numbers] | |
>> squares | |
[1, 4, 9, 16] | |
#example_2 | |
obj = ["Even" if i%2==0 else "Odd" for i in range(5)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lis = (i**2 for i in range(5)) | |
>> type(list) | |
<class 'generator'> | |
>> list(lis) | |
[0, 1, 4, 9, 16] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = [1, 2, 3, 4, 5] | |
list_b = ['a', 'b', 'c', 'd', 'e'] | |
zipped_object = zip(list_a, list_b) | |
>> type(zipped_object) | |
<class 'zip'> | |
>> list(zipped_object) | |
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')] |
OlderNewer