Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!gegeweb.org!news.glorb.com!news-out.octanews.net!indigo.octanews.net!auth.brown.octanews.com.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.python Subject: Re: a better way to invert a list? References: <2215eefd-3677-4459-8656-aa04978f6f3f@g7g2000pro.googlegroups.com> Date: Wed, 06 Apr 2011 12:51:32 -0700 Message-ID: <7xwrj7kvjv.fsf@ruckus.brouhaha.com> Organization: Nightsong/Fort GNOX User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:CD6mcX7SsBdSeyybBKnHoahWhdw= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 19 NNTP-Posting-Date: 06 Apr 2011 14:51:32 CDT X-Complaints-To: abuse@octanews.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2718 scattered writes: > def invert(p): > return [ j for (i,j) in sorted(zip(p,range(len(p))))] return [j for i,j in sorted(enumerate(p), key=itemgetter(1))] looks a little cleaner to me. In Haskell or ML, you can use patterns that contain wild cards that play a role in the pattern-matching but don't establish any binding. Can that be done in Python? Not as much. You could say something like sorted(enumerate(p), key=lambda(_,j): j) which gets the meaning across (it binds the symbol "_" though this doesn't escape the lambda).