Hi Laurent,

It is not clear to me what you mean by "filter by indices".

On Sat, Oct 02, 2021 at 10:25:05PM +0200, Laurent Lyaudet wrote:

> The idea is to filter a list by indices :
[...]
> Since filter() returns an iterator instead of a list, it could do what
> is needed... if the callback had access to the index like the
> Javascript array filter function.

Do you mean this?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

You shouldn't assume we are all experts on Javascript :-)

If that is what you want, it is easy to get access to the index. We can 
just do:

    filter(function, enumerate(items))

and so long as function takes two arguments, it will be fine. It is a 
little bit trickier to get the Javascript three argument version, but 
with a one-liner helper function, it is easy:

    def js_enumerate(sequence):
        for index, obj in enumerate(sequence):
            yield (index, obj, sequence)

    filter(function, js_enumerate(items))


If you want something else, I'm afraid you will have to explain in more 
detail what you want, sorry.



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MFSXBBBKM73KWDYRQYQK6EZTIQS3GUOA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to