Skip to content

Instantly share code, notes, and snippets.

@jeffwright13
Last active December 29, 2022 11:31
Show Gist options
  • Save jeffwright13/d7b8108179091f68fbe35a6d8e51015e to your computer and use it in GitHub Desktop.
Save jeffwright13/d7b8108179091f68fbe35a6d8e51015e to your computer and use it in GitHub Desktop.
Uniquify List (make a list of any type unique)
#Python code to make a list of any type(s) unique
import pytest
from dataclasses imprt dataclass, field
from typing import Any
from uniquify_list import uniquify_list
@pytest.mark.parametrize(
"input",
[
None,
{},
(),
"a",
1.0,
],
)
def test_uniquify_invalid_data(input: Any) -> None:
def cause_type_error(i) -> None:
uniquify_list(i)
with pytest.raises(TypeError):
cause_type_error(input)
@dataclass
class A:
a: int = 0
b: str = "0"
c: float = 0.0
d: bool = False
e: list = field(default_factory=list)
f: dict = field(default_factory=dict)
g: tuple = field(default_factory=tuple)
h: set = field(default_factory=set)
@pytest.mark.parametrize(
"data, expected_result",
[
([0, 0, 0, 0, 0], [0]),
([1, 1, 1], [1]),
([-1, -1, -1], [-1]),
([0, "a", 0, [0], 1, {"zero": 0}, "a", 1], [0, "a", [0], 1, {"zero": 0}]),
([A(), A(), A()], [A()]),
],
)
def test_uniquify_with_valid_data(data, expected_result) -> None:
assert uniquify_list(data) == expected_result
from typing import Any
def uniquify_list(my_list: list[Any]) -> list[Any]:
"""
Remove duplicates from a list containing any types (including nested lists,
dicts, objects). Return the result as a new list.
This is needed because list(set(list)) fails for list elements that are not
hashable (like objects instantiated from a class).
"""
if type(my_list) is not list:
raise TypeError(f"Input must be a list, not {type(my_list)}")
uniques = []
seen = []
for item in my_list:
if not uniques:
uniques.append(item)
seen.append(item)
continue
if item not in seen:
uniques.append(item)
seen.append(item)
return uniques
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment