Hello,

This is a very simple feature request that does not break anything but
I don't know if you may find it interesting.
It would be nice to have a function or method of list objects that does this :
- First idea :
def enumerate_with_rest(my_list):
    for i, item in enumerate(my_list):
        yield i, item, my_list[:i] + my_list[i + 1:]

It could be called easily with:

for i, item, rest in enumerate_with_rest(my_list):
   # do something

or
for i, item, rest in my_list.enumerate_with_rest():
   # do something

I am not the only one who had the same need :
https://stackoverflow.com/questions/56966429/getting-pairs-of-one-item-and-the-rest-over-a-python-list

It would be nice to have an optimized C function for this.
However, it may be less interesting than this :
 - Second idea
enumerate_with_rest above has quadratic complexity.
It is probably true that most processes that use it will also have
quadratic complexity.
However, it would be better to return an iterator instead of a list
for the rest,
it would use less space.
For this a param skip to enumerate would do the job
def enumerate_with_rest(my_list):
    for i, item in enumerate(my_list):
        yield i, item, enumerate(my_list, skip=i)
There could be variants of this idea like :
- enumerate(my_list, skip=i)
- enumerate(my_list, skip=[i])
- enumerate(my_list, filter_callback=(lambda x: x != i))

Please let me know what you think of it :)

Thanks for your time, best regards,
     Laurent Lyaudet
_______________________________________________
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/5LGWV3YLCNBVSL4QHQKJ7RPNTMWOALQA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to