remove

iteration_utilities.remove(iterable, idx=None, start=None, stop=None)

Removes the item at idx or from start (inclusive) to stop (exclusive).

Parameters
iterableiterable

The iterable in which to remove the item(s).

idxpositive int, list/tuple thereof, None, optional

If not None, remove the item at idx. If it’s a tuple or list then replace all the present indices (they will be sorted so only the specified indices are removed). Default is None.

Note

This parameter must not be None if also start and stop are None.

startpositive int or None, optional

If None then remove all items before stop, otherwise remove only the items starting by start. Default is None.

Note

This parameter is ignored if idx is not None.

stoppositive int or None, optional

If None then remove all items starting by start, otherwise only remove the items before stop. Default is None.

Note

This parameter is ignored if idx is not None.

Returns
replacedgenerator

The iterable with the specified items removed.

Examples

To remove one item:

>>> from iteration_utilities import remove
>>> list(remove(range(10), idx=2))
[0, 1, 3, 4, 5, 6, 7, 8, 9]

To remove several items just provide a tuple as idx (the values are sorted, so exactly the specified elements are removed):

>>> list(remove(range(10), (4, 6, 8, 5, 1)))
[0, 2, 3, 7, 9]

To remove a slice:

>>> list(remove(range(10), start=2))
[0, 1]

>>> list(remove(range(10), stop=2))
[2, 3, 4, 5, 6, 7, 8, 9]

>>> list(remove(range(10), start=2, stop=5))
[0, 1, 5, 6, 7, 8, 9]