Perl's @foo[3,7,1,-1] ?

John Yeung gallium.arsenide at gmail.com
Sat Jun 13 17:27:37 EDT 2009


On Jun 13, 3:59 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> kj <no.em... at please.post> writes:
> > Are these the most idiomatically pythonic forms?  Or am
> > I missing something better?
>
> You're missing operator.itemgetter:
>
> >>> from operator import itemgetter
> >>> foo = "spam & eggs"
> >>> itemgetter(3, 7, 1, -1)(foo)
>
> ('m', 'e', 'p', 's')

That looks to me like the best solution to the OP's specific
question.  It's amazing how many cool things are tucked into the
standard library.  While it's easy to miss these things, I appreciate
the effort to keep Python's core relatively small.  (Not knowing about
itemgetter, I would have gone with the list comprehension.)

John



More information about the Python-list mailing list