a better way to invert a list?

Raymond Hettinger python at rcn.com
Wed Apr 6 02:07:45 CEST 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