retain_elements

sequencetools.retain_elements(sequence, indices=None, period=None)

Retains sequence elements.

Sequence for examples:

>>> sequence = list(range(15))

Example 1. Retains all elements:

>>> sequencetools.retain_elements(sequence)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Example 2. Retains elements at indices 2 and 3:

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[2, 3],
...     )
[2, 3]

Example 3. Retains elements at indices -2 and -3:

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[-2, -3],
...     )
[12, 13]

Example 4. Retains elements at indices congruent to 2 or 3 (mod 4):

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[2, 3],
...     period=4,
...     )
[2, 3, 6, 7, 10, 11, 14]

Example 5. Retains elements at indices congruent to -2 or -3 (mod 4):

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[-2, -3],
...     period=4,
...     )
[0, 1, 4, 5, 8, 9, 12, 13]

Example 6. Retains no elements:

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[],
...     )
[]

Example 7. Retains no elements:

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[97, 98, 99],
...     )
[]

Example 8. Retains no elements:

>>> sequencetools.retain_elements(
...     sequence,
...     indices=[-97, -98, -99],
...     )
[]

Returns elements in the order they appear in sequence.

Returns new object of sequence type.