python list pattern matching?

guthrie guthrie at mum.edu
Thu May 28 18:43:26 EDT 2009


I want to do a functional like pattern match to get teh first two
elements, and then the rest of an array return value.

For example, assume that perms(x) returns a list of values, and I want
to do this:
    seq=perms(x)

    a = seq[0]
    b = seq[1]
    rest = seq[2:]
Of course I can shorten to:
    [a,b] = seq[0:2]
    rest  = seq[2:]

Can I find use some notation to do this?
    [a,b,more] = perms(x)
or conceptually:
    [a,b,more..] = perms(x)

PROLOG & functional languages do list decomposition so nicely like
this!



More information about the Python-list mailing list