From 2939aa9c4b5f88a7d3e109082a64878f30ba86c2 Mon Sep 17 00:00:00 2001 From: douglas-raillard-arm Date: Wed, 15 Jul 2020 17:06:15 +0100 Subject: [PATCH] lisa.utils: Fix deduplicate() The current behavior does keep the occurence it's asked to keep, but inserts them at the wrong position in the output. --- lisa/utils.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lisa/utils.py b/lisa/utils.py index 232822ebf..c2d118ce8 100644 --- a/lisa/utils.py +++ b/lisa/utils.py @@ -858,13 +858,17 @@ def deduplicate(seq, keep_last=True, key=lambda x: x): items. Otherwise, keep the first occurence. :type keep_last: bool """ - reorder = (lambda seq: seq) if keep_last else reversed - # Use an OrderedDict to keep original ordering of the sequence - dedup = OrderedDict( - (key(x), x) - for x in reorder(seq) - ) - return list(reorder(dedup.values())) + reorder = reversed if keep_last else (lambda seq: seq) + + out = [] + visited = set() + for x in reorder(seq): + k = key(x) + if k not in visited: + out.append(x) + visited.add(k) + + return list(reorder(out)) def take(n, iterable): -- GitLab