Suppose I have a list of tuples:
tuple_library = [('a', 'z', '1'), ('r', '3', 'b'), ('m', '1', 'l')]
What I want to do is to check if the following tuple is present on the tuple_library.
search_list = [('a','a','1'), ('m', '1', 'l')]
def search_the_tupple(t_lib, s_list):
for item in t_lib:
if item in s_list:
return(item)
print(search_the_tupple(tuple_library, search_list))
this code works ok if the tuple_library and the search_list is small, but as those two item increases, the longer time needed in order for it to complete.
How do we approach this problem?
tuple_library
and search_list
to a python set with set()
tuple_library
and search_list
tuple_library = [('a', 'z', '1'), ('r', '3', 'b'), ('m', '1', 'l')]
search_list = [('a','a','1'), ('m', '1', 'l')]
def search_the_tupple(t_lib, s_list):
return set(t_lib).intersection(set(s_list))
print(search_the_tupple(tuple_library, search_list))
This is assuming that you want the order of the tuple to be preserved. (so that a ('m', '1', 'l')
would appear but a ('m', 'l', '1')
would not.
Fyi: it does not matter whether you do t_lib.intersection(s_list)
or the other way around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With