Inverse of dict(zip(x,y))

Paul Rubin http
Wed Mar 4 23:15:42 EST 2009


Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> writes:
> Sure, but if you want two lists, as the OP asked for, then you have to 
> iterate over it twice either way:
> 
> # method 1:
> keys = dict.keys()
> values = dict.values()
> 
> # method 2:
> keys, values = zip(*dict.items())
> 
> First you iterate over the dict to get the items, then you iterate over 
> the items to split into two lists. Anyone want to take bets on which is 
> faster?

The first way involves iterating over the dict items twice.  The
second way iterates over the dict items just once, copying them to
another place; it then iterates over the copy.



More information about the Python-list mailing list