+
+
+def prepend(item: Any, lst: List[Any]) -> List[Any]:
+ """
+ Prepend an item to a list.
+
+ >>> prepend('foo', ['bar', 'baz'])
+ ['foo', 'bar', 'baz']
+
+ """
+ lst.insert(0, item)
+ return lst
+
+
+def population_counts(lst: List[Any]) -> Mapping[Any, int]:
+ """
+ Return a population count mapping for the list (i.e. the keys are
+ list items and the values are the number of occurrances of that
+ list item in the original list.
+
+ >>> population_counts([1, 1, 1, 2, 2, 3, 3, 3, 4])
+ Counter({1: 3, 3: 3, 2: 2, 4: 1})
+
+ """
+ return Counter(lst)
+
+
+def most_common_item(lst: List[Any]) -> Any:
+
+ """
+ Return the most common item in the list. In the case of ties,
+ which most common item is returned will be random.
+
+ >>> most_common_item([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4])
+ 3
+
+ """
+ return population_counts(lst).most_common(1)[0][0]
+
+
+def least_common_item(lst: List[Any]) -> Any:
+ """
+ Return the least common item in the list. In the case of
+ ties, which least common item is returned will be random.
+
+ >>> least_common_item([1, 1, 1, 2, 2, 3, 3, 3, 4])
+ 4
+
+ """
+ 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):
+ """
+ Return the ngrams in the sequence.
+
+ >>> seq = 'encyclopedia'
+ >>> for _ in ngrams(seq, 3):
+ ... _
+ 'enc'
+ 'ncy'
+ 'cyc'
+ 'ycl'
+ 'clo'
+ 'lop'
+ 'ope'
+ 'ped'
+ 'edi'
+ 'dia'
+
+ >>> seq = ['this', 'is', 'an', 'awesome', 'test']
+ >>> for _ in ngrams(seq, 3):
+ ... _
+ ['this', 'is', 'an']
+ ['is', 'an', 'awesome']
+ ['an', 'awesome', 'test']
+ """
+ for i in range(len(lst) - n + 1):
+ yield lst[i:i + n]
+
+
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()