Commit f7414041 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Add function to return list with unique elements.

Reviewed-by: ultrotter
parent 7d7609a3
......@@ -1002,3 +1002,12 @@ def any(seq, pred=bool):
for elem in itertools.ifilter(pred, seq):
return True
return False
def UniqueSequence(seq):
"""Returns a list with unique elements.
Element order is preserved.
"""
seen = set()
return [i for i in seq if i not in seen and not seen.add(i)]
......@@ -608,5 +608,28 @@ class TestNewUUID(unittest.TestCase):
self.failUnless(self._re_uuid.match(utils.NewUUID()))
class TestUniqueSequence(unittest.TestCase):
"""Test case for UniqueSequence"""
def _test(self, input, expected):
self.assertEqual(utils.UniqueSequence(input), expected)
def runTest(self):
# Ordered input
self._test([1, 2, 3], [1, 2, 3])
self._test([1, 1, 2, 2, 3, 3], [1, 2, 3])
self._test([1, 2, 2, 3], [1, 2, 3])
self._test([1, 2, 3, 3], [1, 2, 3])
# Unordered input
self._test([1, 2, 3, 1, 2, 3], [1, 2, 3])
self._test([1, 1, 2, 3, 3, 1, 2], [1, 2, 3])
# Strings
self._test(["a", "a"], ["a"])
self._test(["a", "b"], ["a", "b"])
self._test(["a", "b", "a"], ["a", "b"])
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment