Binary Sort on Python List __xor__

Terry Reedy tjreedy at udel.edu
Sun May 31 22:37:16 EDT 2020


On 5/31/2020 12:24 PM, Evan Schalton wrote:

> I'm less strictly interested in the & operator explicitly working with a bit int, but think it'd be great if the was a built-in filter something like:

> [1,2,3,4] & [0,0,1,1] => [3,4] OR
> [1,2,3,4] & [False, False, True, True] = [3,4]

Leaving numpy aside, Python already has very flexible map and filter 
functions, and comprehensions encompassing both, that easily do both 
what *you* want:

 >>> list(map(lambda x: x[0], filter(lambda x: x[1], zip([1,2,3,4], 
[0,0,1,1]))))
[3, 4]
 >>> [x[0] for x in zip([1,2,3,4], [0,0,1,1]) if x[1]]
[3, 4]
# This illustrates why comprehensions were added.

and an infinity of related operations, that *others* may want or can 
imagine, such as:

 >>> ''.join(c[1] for c in enumerate('every third char') if not c[0] % 3)
'ertrcr'

Python 3, even more than Python 2, is designed to work with generic 
sequences and streams rather than just lists.
We would only add a specific list function if the function is specific 
to lists, and filtering a sequence of items according to a sequence of 
values treated as boolean values is definitely not.  Please drop the idea.

-- 
Terry Jan Reedy



More information about the Python-list mailing list