MCPcopy Index your code
hub / github.com/webpy/webpy / uniq

Function uniq

web/utils.py:555–577  ·  view source on GitHub ↗

Removes duplicate elements from a list while preserving the order of the rest. >>> uniq([9,0,2,1,0]) [9, 0, 2, 1] The value of the optional `key` parameter should be a function that takes a single argument and returns a key to test the uniqueness. >>> uniq(["F

(seq, key=None)

Source from the content-addressed store, hash-verified

553
554
555def uniq(seq, key=None):
556 """
557 Removes duplicate elements from a list while preserving the order of the rest.
558
559 >>> uniq([9,0,2,1,0])
560 [9, 0, 2, 1]
561
562 The value of the optional `key` parameter should be a function that
563 takes a single argument and returns a key to test the uniqueness.
564
565 >>> uniq(["Foo", "foo", "bar"], key=lambda s: s.lower())
566 ['Foo', 'bar']
567 """
568 key = key or (lambda x: x)
569 seen = set()
570 result = []
571 for v in seq:
572 k = key(v)
573 if k in seen:
574 continue
575 seen.add(k)
576 result.append(v)
577 return result
578
579
580def iterview(x):

Callers

nothing calls this directly

Calls 2

addMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected