So if you had the string

POST /wfs?request=Null&log&foo:bar=1,2:3,2:4,2&baz=x,y:y,z

and wanted to turn it into the list

['foo:bar:1,2', 'foo:bar:3,2', 'foo:bar:4,2', 'baz:x,y', 'baz:y,z']

How could you do it? Well, you could define a grammar & pass that into a parser… or you could have a pile or for loops…. Or, how about a triply-nested list comprehension contained within a reduce call? Like this!!!

reduce(operator.add,       [[(tag + ':' + value) for value in values.split(':')]        for (tag, values)        in [b.split('=') for b in a.split('&')[2:]]])
Written by