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)
| 553 | |
| 554 | |
| 555 | def 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 | |
| 580 | def iterview(x): |