[Python-ideas] [Python-Dev] Overloading comparison operator for lists

Mike Müller mmueller at python-academy.de
Mon May 27 03:44:33 EDT 2019


Am 27.05.19 um 06:12 schrieb Cameron Simpson:

> On 26May2019 21:52, Montana Burr <montana.burr at gmail.com> wrote:
>> NumPy arrays have this awesome feature, where array == 3 does an
>> element-wise comparison and returns a list. For example:
>>
>> np.array([1,2,3,4,5])==3
>>
>> returns
>>
>> [False,False,True,False,False]
>> It would be cool if Python had similar functionality for lists.
> 
> map(lamdba item: item==3, [1,2,3,4,5])

A list comprehension seems more pythonic to me:

[item == 3 for item in [1, 2, 3, 4, 5]]

or, if you want it to make it lazy, use generator expression:

(item == 3 for item in [1, 2, 3, 4, 5])

The latter is the direct equivalent to `map()` (in Python 3).


More information about the Python-ideas mailing list