Skip to content

Commit 58e22bb

Browse files
authored
Merge pull request #4737 from branfosj/nub
faster `nub` function
2 parents 52d3f9e + 6590fd4 commit 58e22bb

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

easybuild/tools/utilities.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,18 @@ def trace_msg(message, silent=False):
204204

205205

206206
def nub(list_):
207-
"""Returns the unique items of a list of hashables, while preserving order of
208-
the original list, i.e. the first unique element encoutered is
209-
retained.
207+
"""Returns the unique items of a list of hashables, while preserving order of the original list,
208+
i.e. the first unique element encoutered is retained.
210209
211-
Code is taken from
212-
http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order
210+
Code is taken from https://www.peterbe.com/plog/fastest-way-to-uniquify-a-list-in-python-3.6
213211
214-
Supposedly, this is one of the fastest ways to determine the
215-
unique elements of a list.
212+
Supposedly, this is one of the fastest ways to determine the unique elements of a list.
216213
217214
@type list_: a list :-)
218215
219216
:return: a new list with each element from `list` appearing only once (cfr. Michelle Dubois).
220217
"""
221-
seen = set()
222-
seen_add = seen.add
223-
return [x for x in list_ if x not in seen and not seen_add(x)]
218+
return list(dict.fromkeys(list_))
224219

225220

226221
def unique_ordered_extend(base, affix):

0 commit comments

Comments
 (0)