Looks like a job for set.isdisjoint
:
input_list = ["a", "b2","d"]
ref_list = [['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']]
res = list(filter(set(input_list).isdisjoint, ref_list))
print(res)
Output (Try it online!):
[['c1', 'c2', 'c3', 'c4']]
Benchmark with your small data:
2.6 μs 2.6 μs 2.7 μs python_user
2.2 μs 2.2 μs 2.2 μs Uttam_Velani
0.9 μs 0.9 μs 0.9 μs dont_talk_just_code
Benchmark with a 1000 times longer ref_list
:
2150 μs 2184 μs 2187 μs python_user
1964 μs 1981 μs 1990 μs Uttam_Velani
292 μs 304 μs 306 μs dont_talk_just_code
Benchmark code (Try it online!):
from timeit import repeat
def python_user(input_list, ref_list):
return [i for i in ref_list if all(j not in i for j in input_list)]
def Uttam_Velani(input_list, ref_list):
required_list = []
for data in ref_list:
if len(set(data) & set(input_list)) == 0:
required_list.append(data)
return required_list
def dont_talk_just_code(input_list, ref_list):
return list(filter(set(input_list).isdisjoint, ref_list))
funcs = python_user, Uttam_Velani, dont_talk_just_code
def test(input_list, ref_list, number, format_time):
expect = funcs[0](input_list, ref_list)
for func in funcs:
result = func(input_list, ref_list)
print(result == expect, func.__name__)
print()
for _ in range(3):
for func in funcs:
times = sorted(repeat(lambda: func(input_list, ref_list), number=number))[:3]
print(*(format_time(t / number) for t in times), func.__name__)
print()
test(["a", "b2","d"],
[['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']],
10000,
lambda time: '%4.1f μs ' % (time * 1e6))
test(["a", "b2","d"],
[['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']] * 1000,
20,
lambda time: '%4d μs ' % (time * 1e6))