Chris Angelico <ros...@gmail.com>:

> Or, even more likely and even more Pythonic:
>
>>>> [fields[i] for i in selector]
> ['y', 'y', 'x']
>
> As soon as you get past the easy and obvious case of an existing
> function, filter and map quickly fall behind comprehensions in utility
> and readability.

The general need is contexts where you need fields[?] act as a function.
Of course,

   lambda i: fields[i]

does it. However, weirdly, dicts have get but lists don't.

Ok, dict.get() provides for a default value, but couldn't that come in
handy for lists as well?

Again, lambda would do it for both dicts and lists:

   lambda i: fields[i] if i >= 0 and i < len(fields) else default

   lambda key: fields[key] if key in fields else default


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to