a better way to invert a list?

Raymond Hettinger python at rcn.com
Tue Apr 5 20:07:45 EDT 2011


[Ian Kelly]
> Which is O(n).  If that is too verbose, you could also use a dictionary:
>
> def invert(p):
>     return dict(map(reversed, enumerate(p)))


def inv(p):
    return dict(zip(p, itertools.count()))


Raymond



More information about the Python-list mailing list