Skip to content

Instantly share code, notes, and snippets.

View Kush1101's full-sized avatar
🤔
<Comprehending>

Kushagra Bansal Kush1101

🤔
<Comprehending>
View GitHub Profile
def prefix_of_list(array):
ans = [[]]
prev = ans[-1]
for i in range(len(array)):
ans.append(prev + array[i])
return ans
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
"""
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]
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
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:
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 = ' ')
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
"""
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
"""
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
# Script to download any video from youtube (without advanced optional paramters)
import youtube_dl
link = 'link_to_youtube_video'
try:
with youtube_dl.YoutubeDL() as ydl:
ydl.download(link)
print('Video downloaded successfully')
except:
print('Some error occoured')