a better way to invert a list?

Glazner yoavglazner at gmail.com
Wed Apr 6 10:48:30 CEST 2011


> > def invert(p):
> >     inverse = [None] * len(p)
> >     for (i, j) in enumerate(p):
> >         inverse[j] = i
> >     return inverse
>
> Elegant. This seems like the best solution, although it isn't as much
> fun to write as a "one-liner". Thanks


>>> invert([1, 2, 3, 1])
[None, 3, 1, 2] #blah



More information about the Python-list mailing list