If this returns true,
>>> "en" in "english"
True
Why doesn't this?
>>> [1,2] in [1,2,3,4]
False
If this returns true,
>>> "en" in "english"
True
Why doesn't this?
>>> [1,2] in [1,2,3,4]
False
Strings and lists are simply different. a in b
is implemented by b.__contains__(a)
. __contains__
is defined on a class-by-class basis.
When b
is a str
,
str.__contains__(b, a)
returns true if a
is a substring of b
.
When b
is a list
,
list.__contains__(b, a)
returns true if b
is an element of a
.
In order to check if a list a
is a subsequence of a list b
, you could try something like
any(a == b[i:i+len(a)] for i, _ in enumerate(b[:-len(a)+1]))
This will give you true: >>>1 and 2 in [1,2,3,4]
But [1,2] means that you are checking for the list [1,2] in the list [1,2,3,4] which is not there.
It would be true if your list was [[1,2],3,4]
.
>>> [1,2] in [[1,2],3,4]
True