forked from beba/foerderbarometer
25 lines
633 B
Python
25 lines
633 B
Python
from typing import Iterable
|
|
|
|
|
|
def reorder_value(values: Iterable, value, *, after=None, before=None):
|
|
"""
|
|
Reorders a value after or before another value in the given list.
|
|
Does not work properly for duplicate or None values.
|
|
Raises ValueError when any of the values is not contained in the list.
|
|
"""
|
|
|
|
assert (after is None) != (before is None), 'Either after or before is needed but not both.'
|
|
|
|
values = list(values)
|
|
|
|
values.remove(value)
|
|
|
|
if after is None:
|
|
index = values.index(before)
|
|
else:
|
|
index = values.index(after) + 1
|
|
|
|
values.insert(index, value)
|
|
|
|
return values
|