changes
[python_utils.git] / list_utils.py
index 533317eb6da71f44a4f8b3c338505c053c460c0b..182e2bc5c104908f39a15e4675021e6ed8a7c338 100644 (file)
@@ -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()