X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=list_utils.py;h=182e2bc5c104908f39a15e4675021e6ed8a7c338;hb=791755ededfc0de84a81b4c7313830efbd27bf8c;hp=533317eb6da71f44a4f8b3c338505c053c460c0b;hpb=c41e0e59446412511c5737cf5b6ba8f289e75e7e;p=python_utils.git diff --git a/list_utils.py b/list_utils.py index 533317e..182e2bc 100644 --- a/list_utils.py +++ b/list_utils.py @@ -2,7 +2,7 @@ from collections import Counter from itertools import islice -from typing import Any, Iterator, List, Mapping +from typing import Any, Iterator, List, Mapping, Sequence def shard(lst: List[Any], size: int) -> Iterator[Any]: @@ -86,6 +86,30 @@ def least_common_item(lst: List[Any]) -> Any: return population_counts(lst).most_common()[-1][0] +def dedup_list(lst: List[Any]) -> List[Any]: + """ + Remove duplicates from the list performantly. + + >>> dedup_list([1, 2, 1, 3, 3, 4, 2, 3, 4, 5, 1]) + [1, 2, 3, 4, 5] + + """ + return list(set(lst)) + + +def uniq(lst: List[Any]) -> List[Any]: + """ + Alias for dedup_list. + + """ + return dedup_list(lst) + + +def ngrams(lst: Sequence[Any], n): + for i in range(len(lst) - n + 1): + yield lst[i:i + n] + + if __name__ == '__main__': import doctest doctest.testmod()