Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion manim/utils/iterables.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def list_difference_update(l1: Iterable[T], l2: Iterable[T]) -> list[T]:
>>> list_difference_update([1, 2, 3, 4], [2, 4])
[1, 3]
"""
l2 = set(l2)
return [e for e in l1 if e not in l2]


Expand All @@ -158,7 +159,8 @@ def list_update(l1: Iterable[T], l2: Iterable[T]) -> list[T]:
>>> list_update([1, 2, 3], [2, 4, 4])
[1, 3, 2, 4, 4]
"""
return [e for e in l1 if e not in l2] + list(l2)
l2 = list(l2)
return list_difference_update(l1, l2) + l2


@overload
Expand Down
35 changes: 35 additions & 0 deletions tests/module/utils/test_iterables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from manim.utils.iterables import list_difference_update, list_update


def test_list_difference_update_removes_matching_items():
assert list_difference_update([1, 2, 3, 4], [2, 4]) == [1, 3]


def test_list_difference_update_preserves_duplicates_and_order():
assert list_difference_update([3, 1, 3, 2, 1], [1]) == [3, 3, 2]


@pytest.mark.parametrize(
("l1", "l2", "expected"),
[([1, 2, 3, 4], [2, 4], [1, 3]), ([], [1, 2], [])],
)
def test_list_difference_update_removes_elements(l1, l2, expected):
assert list_difference_update(l1, l2) == expected


def test_list_difference_update_preserves_l1_order_and_duplicates():
assert list_difference_update([3, 1, 3, 2, 1], [1]) == [3, 3, 2]


@pytest.mark.parametrize(
("l1", "l2", "expected"),
[([1, 2, 3], [2, 4], [1, 3, 2, 4]), ([], [1, 2], [1, 2])],
)
def test_list_update_removes_overlap_and_appends_l2(l1, l2, expected):
assert list_update(l1, l2) == expected


def test_list_update_preserves_duplicates_in_l2():
assert list_update([1, 2, 3], [2, 4, 4]) == [1, 3, 2, 4, 4]