I have a list of objects, and want to check if part of the list matches a specific pattern.
Consider the following lists:
l1 = ["foo", "bar"]
l2 = [{1, 2},"foo", "bar"]
l3 = ["foo", "bar", 5]
l4 = [{1,2},"foo", "bar", 5, 6]
How would I match the sequence ["foo", "bar"] in all the different cases?
My naive idea is:
match l4:
case [*_, "foo", "bar", *_]:
print("matched!")
Unfortunately this is a SyntaxError: multiple starred names in sequence pattern.
The issue is, that I don't know how many elements are leading and trailing the pattern.
Edit:
I think I need to clarify: "foo", "bar" is just a stand-in for a much more complex pattern. (I am working with an AST object)
PS: I just found a beautiful recursive solution here (recursion is always beautiful... if your list is not too long)