I'm aware Python's zip() function is a builtin function written in C but I'm confused as to why they have bothered.
If I'm not mistaken, in Python, zip() can be rewritten as:
def zip(seq1, seq2):
out = []
for i in range(len(seq1)):
out.append((seq1[i], seq2[i]))
return out
(assuming seq1 and seq2 are the same size. The actual zip() will use the length of the smallest sequence)
This is a very simple piece of code, so I'm wondering, why have they made zip() a builtin function? I can only assume writing it in C is to make it faster - if that is the case, does anybody know how much faster? (I'm aware this will depend on the size seq1 and seq2)
You can see that there is execution time difference(more than twice) in both
zippedand inbuildzipfunction.While
itertools.iziptakes same about that ofzip,even for large data array:-