I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:
fun (head : rest) = ...
So when I pass in a list, head
will be the first element, and rest
will be the trailing elements.
Likewise, in Python, I can automatically unpack tuples:
(var1, var2) = func_that_returns_a_tuple()
I want to do something similar with lists in Python. Right now, I have a function that returns a list, and a chunk of code that does the following:
ls = my_func()
(head, rest) = (ls[0], ls[1:])
I wondered if I could somehow do that in one line in Python, instead of two.