1. Jason R. Coombs
  2. jaraco.util

Commits

Jason R. Coombs  committed 6e05ed9

Adding unique_everseen

  • Participants
  • Parent commits 292e924
  • Branches default
  • Tags 6.7

Comments (0)

Files changed (2)

File README

View file
 Changes
 -------
 
+6.7
+~~~
+
+* Added `itertools.unique_everseen` from Python docs.
+
 6.6
 ~~~
 

File jaraco/util/itertools.py

View file
 			itertools.groupby(iterable, key)
 		))
 
+# from Python 3.3 documentation
+def unique_everseen(iterable, key=None):
+	"""
+	List unique elements, preserving order. Remember all elements ever seen.
+
+	>>> print(' '.join(unique_everseen('AAAABBBCCDAABBB')))
+	A B C D
+
+	>>> print(' '.join(unique_everseen('ABBCcAD', unicode.lower)))
+	A B C D
+	"""
+	seen = set()
+	seen_add = seen.add
+	if key is None:
+		for element in itertools.ifilterfalse(seen.__contains__, iterable):
+			seen_add(element)
+			yield element
+	else:
+		for element in iterable:
+			k = key(element)
+			if k not in seen:
+				seen_add(k)
+				yield element
+
 def every_other(iterable):
 	"""
 	Yield every other item from the iterable