From 8aaafe408eb1629b0a4a1eb094186b5f290e31b1 2013-01-25 18:41:01 From: W. Trevor King Date: 2013-01-25 18:41:01 Subject: [PATCH] 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 --- diff --git a/IPython/utils/data.py b/IPython/utils/data.py index 2b7a404..69811d9 100644 --- a/IPython/utils/data.py +++ b/IPython/utils/data.py @@ -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):