This will not work because list a and b are not the same length ...
[php]a = [1, 2]
b = [2, 3, 6, 1]
# this will not work here ...
if sorted(a) == sorted(b):
print 'a is in b'
else:
print 'a is not in b'
[/php]
Using set() will work, like mawe said ...
[php]a = [1, 2]
b = [2, 3, 6, 1]
# this will work ...
if set(a).issubset(set(b)):
print 'a is in b'
else:
print 'a is not in b'
[/php]The next approach is pretty universal ...
[php]def is_a_in_b(a, b):
"""returns True if all elements in list a are in list b, else False"""
ax = []
for x in a:
for y in b:
if x == y:
ax.append(y)
return a == ax
print is_a_in_b([1, 2], [2, 3, 6, 1]) # True
print is_a_in_b([1, 2], [1, 3, 7, 9, 4]) # False
[/php]