Unzip Un-needed in Python?

I recently read a post that contends that an unzip function is unneeded in python. His argument is that you don't need unzip because of the following:

>>> t1 = (0,1,2,3)
>>> t2 = (7,6,5,4)
>>> [t1,t2] == zip(*zip(t1,t2))
True

Oh, of course! How intuitive! OK, no more sarcasm. To me that seems to be very un-pythonic. I never would have thought to use *zip() inside of zip() to get the unzipped version. I would however have expected to be able to write unzip() and get a result. I decided to see how easy it would be to create an unzip() of my own so I fired this off just to see.

def unzip(l):
    if len(l) < 1:
        return []
    
    itemCount = len(l[0])
    
    unzipped = []
    
    for i in range(itemCount):
        unzipped.append(tuple([j[i] for j in l]))
    
    return unzipped

if __name__ == '__main__':
    
    t1 = ("brian","becca","hyrum","david")
    t2 = (29,30,3,27)
    t3 = ("utah","utah","utah","arizona")
    
    zipped = zip(t1,t2,t3)
    unzipped = unzip(zipped)
    
    print zipped
    print unzipped

Comments