Optimize unique_justseen() recipe for a common case. (gh-113147)

This commit is contained in:
Raymond Hettinger 2023-12-14 17:27:39 -06:00 committed by GitHub
parent 25061f5c98
commit 5f7d7353b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1017,6 +1017,8 @@ which incur interpreter overhead.
"List unique elements, preserving order. Remember only the element just seen." "List unique elements, preserving order. Remember only the element just seen."
# unique_justseen('AAAABBBCCDAABBB') --> A B C D A B # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
# unique_justseen('ABBcCAD', str.lower) --> A B c A D # unique_justseen('ABBcCAD', str.lower) --> A B c A D
if key is None:
return map(operator.itemgetter(0), groupby(iterable))
return map(next, map(operator.itemgetter(1), groupby(iterable, key))) return map(next, map(operator.itemgetter(1), groupby(iterable, key)))