How to divide a list into a list of smaller lists based on a predicate?

Viewed 647

Let's say I have a list like this:

[('Yadda', 5), ('Yadda', 9), ('Blah', 12), ('Blah', 2), ('Blah', 4)]

And I'd like to convert it to a list like this:

[ [('Yadda', 5), ('Yadda', 9)], [('Blah', 12), ('Blah', 2), ('Blah', 4)] ]

Assuming the list is sorted wrt the predicate on which it should be split -

What's the Pythonic way of doing this?

Is there any function that does this or do I have to write it myself?

5 Answers
Related