Jul-18-2021, 06:30 PM
Iterative way, if order of sequence is important:
# Generator - lazy evaluated # must be consumed after called, e.g. from list() or a for loop def compare(exclude, sequence): for element in sequence: if element not in exclude: yield element to_exclude = [1,3,5,6,7] sequence = [1,2,3,4,5,6,7,8,9,10] # comsume the generator missing_values = list(compare(to_exclude, sequence)) print(missing_values)
Output:[2, 4, 8, 9, 10]
Or the classic way:def compare(exclude, sequence): result = [] for element in sequence: if element not in exclude: result.append(element) return result to_exclude = [1,3,5,6,7] sequence = [1,2,3,4,5,6,7,8,9,10] # this time a normal function which returns a list with results missing_values = compare(to_exclude, sequence) print(missing_values)
Output:[2, 4, 8, 9, 10]
Another approach, if the order doesn't matter, are set
s.to_exclude = set([1,3,5,6,7]) sequence = set([1,2,3,4,5,6,7,8,9,10]) missing_values = sequence - to_exclude print(missing_values)
Output:{2, 4, 8, 9, 10}
More about set: https://realpython.com/python-sets/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!