I have
x = [
{"foo": "bar"},
{"bar": "baz"},
]
y = [
{"bar": "baz"},
{"foo": "bar"},
]
for item in (x, y):
match item:
case [{"foo": "bar"}, *other_items]:
print(f"Caught {item}")
case _:
print(f"Did not catch {item}")
This yields the following output:
Caught [{'foo': 'bar'}, {'bar': 'baz'}]
Did not catch [{'bar': 'baz'}, {'foo': 'bar'}]
Is there a way to redefine the pattern [{"foo": "bar"}, *other_items]
so to catch the value for both x
and y
?
Please note that I would really like to use pattern matching to solve this problem.