How to show a dictionary sorted on a value within its data?

Chris Angelico rosuav at gmail.com
Fri Oct 3 04:44:32 CEST 2014


On Fri, Oct 3, 2014 at 9:16 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> Anyway, pylint doesn't complain about a bare use of lambda, but it
>> does complain about a map applied to a lambda or a filter applied to a
>> lambda.  Pylint says they could be replaced by a list comprehension,
>> with the warning "deprecated-lambda".
>
> The warning name is misleading, lambda is not deprecated. But stylistically,
> many people prefer to use a generator expression in place of map or filter:
>
> # instead of this
> map(lambda x: 2*x+1, values)
>
> # use this
> (2*x+1 for x in values)
>
> # instead of this
> filter(lambda x: x > 23, values)
>
> # use this
> (x for x in values if x > 23)

Don't forget that your equivalencies are based on the Py3 map/filter,
which return non-list iterables. There's no really convenient
equivalent to the map() usage of "call this function with each of
these args, and discard the return values", as it looks very odd to do
a list comp for nothing:

[print(x) for x in list_of_strings]

Short of borrowing Pike's automap syntax:

print(list_of_strings[*])

there's really not much that's better than the original map() call. On
the other hand, there aren't actually all that many times when I've
needed to do this, and the simple for loop generally suffices:

for x in list_of_strings: print(x)

So it's not a huge deal.

ChrisA



More information about the Python-list mailing list