if lista1 in lista2 :
print('todos dentro')
What you compared here is not whether one list contains all the elements of the other, but whether one list contains the other. These are separate things.
a = [1, 2]
, b = [1, 2, 3]
(a in b
=> False
)
a = [1, 2]
, b = [[1, 2], 3]
(a in b
=> True
)
In the situation (1) the list b
contains all elements of a
; in the situation (2) the list b
contains the list a
. You need to check the situation (1), but checked the situation (2).
What you can do to check is make a loop through the elements of the first list and check one by one if they are also in the second list. In Python, the simplest way to do this would be:
if all(i in b for i in a):
print('Todos dentro')
I mean, check whether i
is in b
for all i
that belongs to a
.
Or, as commented, you can use set theories. In Python, you can create a set from a list from the structure set
:
a = set([1, 2])
b = set([1, 2, 3])
And so use the method set.issubset
to check if one set is sub-set of another:
if a.issubset(b):
print('Todos dentro')
Or by the operator <=
, who does the same thing:
if a <= b:
print('Todos dentro')
maybe this material can give you a hand
– Gustavo André Richter
You want to apply set theory to lists, have you thought about using ensembles? So I could do
{0, 1, 2, 3} <= {0, 1, 2, 3, 4, 5} == True
– fernandosavio
First, we need to define a detail. The list
[1, 1]
is on the list[1, 2]
? That is, when there are repeated elements, they should be considered the same or distinct element?– Woss
@Andersoncarloswoss Yes, we consider the same element
– Bernardo Martins