This is pretty simple and quite obvious to do. If you want to check if any number is in any list, you do this:
def any_number_in_any_lists(numbers, lists):
for alist in lists:
for number in numbers:
if number in alist:
return True
return False
If you want to check that all numbers are in all lists, you do this:
def all_number_in_all_lists(numbers, lists):
for alist in lists:
for number in numbers:
if number not in alist:
return False
return True
If you want to check for all numbers in any lists, it gets more complicated. The easiest and clearer is to have two methods:
def all_numbers_in_list(l, numbers):
for n in numbers:
if n is not in l:
return False
return True
def all_numbers_in_any_list(lists, numbers):
for l in lists:
if all_numbers_in_list(l, numbers):
return True
return False
And you'd use the functions above like this:
if all_number_in_all_lists([3,5,6], [list1, list2, list3]):
do something
else:
do something else
Since you want less code, there are various "shortcuts" you can use. The shortcuts can make the code shorter, but will also make it less clear and harder to read, and hence they are not really a better choice. For example you can use the any function, and a generator expression:
def any_number_in_any_lists(numbers, lists):
for alist in lists:
if any(number in alist for number in numbers):
return True
return False
You can even nest two of them:
def any_number_in_any_lists(numbers, lists):
return any(any(number in alist for number in numbers) for alist in lists)
But understanding that code takes a long time and much more brain power. It is also significantly slower, as it will go through all lists, even if it finds a match in the first one. I can't think of any way that is not slower at the moment, but there may be one. But you are unlikely to find anything that is significantly faster and not significantly more confusing.
For the case of checking that all numbers are in all lists, you would do this:
def all_number_in_all_lists(numbers, lists):
return all(all(number in alist for number in numbers) for alist in lists)
Which also, for some reason takes twice the time compared with the function above, even though there will be no shortcuts. It may have to do with the creation of intermediary lists.
This is often the case with Python: Simple is best.
Line by line explanation of the code you used, since you asked for it:
def all_numbers_in_list(l, numbers):
Defines a function, called "all_numbers_in_list", with the parameters "l" and "numbers".
for n in numbers:
For every item in numbers, assign variable "n" to that number, and then do the following block.
if n is not in l:
If the value of variable "n" is not in the list "l".
return False
Exit the function with False as return value.
return True
Exit the function with True as return value.
def all_numbers_in_any_list(lists, numbers):
Defines a function, called "all_numbers_in_any_list", with the parameters "lists" and "numbers".
for l in lists:
For every item in lists, assign variable "l" to that number, and then do the following block.
if all_numbers_in_list(l, numbers):
If calling the function "all_numbers_in_list" with the parameters "l" and "numbers" return True, do the following block:
return True
Exit the function with True as return value.
return False
Exit the function with False as return value.
Did that help?