Commit d8c8c082 by Marc Pujol Committed by James Cammarata

Merge and intersect lists without using sets.

Using sets for these operations is dangerous because sets cannot contain
certain object types (such as lists) and their iteration order is
undefined.

Fixes #7596
parent 3801be69
...@@ -1005,21 +1005,19 @@ def is_list_of_strings(items): ...@@ -1005,21 +1005,19 @@ def is_list_of_strings(items):
return False return False
return True return True
def _listify(a):
if not isinstance(a, (list, tuple)):
return [a,]
else:
return a
def list_union(a, b): def list_union(a, b):
set_a = set(_listify(a)) result = list(a)
set_b = set(_listify(b)) for i in b:
return list(set_a.union(set_b)) if i not in result:
result.append(i)
return result
def list_intersection(a, b): def list_intersection(a, b):
set_a = set(_listify(a)) result = []
set_b = set(_listify(b)) for i in a:
return list(set_a.intersection(set_b)) if i in b:
result.append(i)
return result
def safe_eval(expr, locals={}, include_exceptions=False): def safe_eval(expr, locals={}, include_exceptions=False):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment