This file contains 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 prefix_of_list(array): | |
ans = [[]] | |
prev = ans[-1] | |
for i in range(len(array)): | |
ans.append(prev + array[i]) | |
return ans | |
This file contains 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
from itertools import zip_longest | |
iter1 = [1, 2, 3, 4, 5, 6] | |
iter2 = [1, 4, 9, 16, 25] | |
iter3 = [1, 8, 27, 64, 125] | |
for i in zip_longest(iter1, iter2, iter3, fillvalue = 0): | |
print(i) | |
# OUTPUT | |
""" |
This file contains 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
from itertools import tee | |
iterable = [1, 2, 4, 6, 7, 13] | |
for i in tee(iterable, 4): | |
print(list(i)) | |
# OUTPUT | |
""" | |
[1, 2, 4, 6, 7, 13] | |
[1, 2, 4, 6, 7, 13] | |
[1, 2, 4, 6, 7, 13] |
This file contains 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
from itertools import takewhile | |
iterable = [1, 3, 5, 7, 2, 4, 6, 9, 11] | |
predicate = lambda x: x%2==1 | |
for i in takewhile(predicate, iterable): | |
print(i, end = ' ') | |
# OUTPUT | |
""" | |
1 3 5 7 |
This file contains 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
from itertools import groupby | |
a_list = [("Even", 2), | |
("Odd", 5), | |
("Even", 8), | |
("Odd", 3)] | |
iterator = groupby(a_list, key = lambda x:x[0]) | |
for key, group in iterator: |
This file contains 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
from itertools import starmap | |
from operator import add, mul | |
iterable = [(1,3), (2,4), (3,0), (5,6)] | |
for i in starmap(add, iterable): | |
print(i, end = ' ') | |
# 4 6 3 11 | |
for i in starmap(mul, iterable): | |
print(i, end = ' ') |
This file contains 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
from itertools import islice | |
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
for i in islice(l, 1, 9, 1): | |
print(i, end =' ') | |
# OUTPUT | |
""" | |
2 3 4 5 6 7 8 9 | |
""" |
This file contains 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
from itertools import filterfalse | |
iterable = [1, 3, 5, 7, 2, 4, 6, 9, 11] | |
predicate = lambda x: x%2==0 | |
for i in filterfalse(predicate, iterable): | |
print(i, end = ' ') | |
# OUTPUT | |
""" |
This file contains 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
from itertools import dropwhile | |
iterable = [1, 3, 5, 7, 2, 4, 6, 9, 11] | |
predicate = lambda x: x%2==1 | |
for i in dropwhile(predicate, iterable): | |
print(i, end = ' ') | |
# OUTPUT | |
""" | |
2 4 6 9 11 |
This file contains 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
from itertools import cycle | |
count = 0 | |
iterable = ["Python", "is", "awesome."] | |
generator = cycle(iterable) | |
for i in range(6): | |
print(next(generator, end = ' ') | |
# OUTPUT |
NewerOlder