Skip to content

Commit

Permalink
Merge pull request ipython#2849 from wking/uniq_stable-comprehension
Browse files Browse the repository at this point in the history
utils/data: Use list comprehension for uniq_stable()

This should be faster than and explicit for loop.  Also, a set makes more
sense than a dict if we only care about values.  After these changes, the
resulting code is the same as Dave Kirby's uniqify() suggestion on Peter
Bengtsson's blog [1].

[1]: http://www.peterbe.com/plog/uniqifiers-benchmark
  • Loading branch information
minrk committed Jan 26, 2013
2 parents 8301f5d + b1b1e32 commit 444b856
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions IPython/utils/data.py
Expand Up @@ -25,21 +25,11 @@ def uniq_stable(elems):
Return from an iterable, a list of all the unique elements in the input,
but maintaining the order in which they first appear.
A naive solution to this problem which just makes a dictionary with the
elements as keys fails to respect the stability condition, since
dictionaries are unsorted by nature.
Note: All elements in the input must be valid dictionary keys for this
routine to work, as it internally uses a dictionary for efficiency
reasons."""

unique = []
unique_dict = {}
for nn in elems:
if nn not in unique_dict:
unique.append(nn)
unique_dict[nn] = None
return unique
Note: All elements in the input must be hashable for this routine
to work, as it internally uses a set for efficiency reasons.
"""
seen = set()
return [x for x in elems if x not in seen and not seen.add(x)]


def sort_compare(lst1, lst2, inplace=1):
Expand Down

0 comments on commit 444b856

Please sign in to comment.