Home>

### i want to determine if the contents of a list are contained in another list in python

I want to know

I want to know if the contents of list 1 [i] are included in list 2 [k] in Python.

``````for i in range (5):
for k in range (5):
if list1 [i] in list2 [k]:``````

I tried it like this, but I got an error.
How can I write to determine if list 2 [k] contains list 1 [i]?

Appendix

``````a = [1,2,3,4,6]
b = [1,3,4,5,6]
for i in range (5):
for k in range (5):
if a [i] in b [k]:
print (i, k)``````

It was useless with this code. What is wrong?

How can I write to determine whether list 1 [i] is included in list 2 [k]?

If you interpret it literally, there is no problem with the code provided.
If there is a problem, there is no body in the if block.

``````for i in range (5):
for k in range (5):
if list 1 [i] in list 2 [k]:
print (f '{list 2 [k]} includes {list 1 [i]}.')``````

Is it easier to understand the purpose if a specific list is illustrated?

After editing the question

Is the use of the in operator wrong?`Element in sequence`, basically.

``````>>>lst1 = [1, 2, 3, 4, 5]
>>>lst2 = [1, 2, 3, 4, 5]
>>>
>>>lst1 [0] in lst2
True
>>>
>>>lst1 [0] in lst2 [0]
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
TypeError: argument of type 'int' is not iterable
>>>
>>>lst1 [0] == lst2 [0]
True``````

Would you like this code?

``````for i in range (5):
for k in range (5):
if a [i] == b [k]:
print (i, k)``````

or

``````for i in range (5):
if a [i] in b:
print (i, b.index (a [i]))``````

If list b has duplicate elements, the behavior is slightly different.