Splitting list of dictionary into sublists after the occurence of particular key of dictionary

Viewed 1143

I have list of dictionaries. These dictionaries basically have just one key-value each.

For example:

lst = [{'x': 23}, {'y': 23432}, {'z': 78451}, {'a': 564}, {'x': 45},
       {'y': 7546}, {'a': 4564}, {'x': 54568}, {'y': 4515}, {'z': 78457}, 
       {'b': 5467}, {'a': 784}]

I am trying to divide the list of dictionaries lst into sublists after every occurrence of a dictionary with a specific key "a".

I tried using other ways that I saw on the internet but as I am new to python, I am not able to understand them and get the desired result. I want the final result to look like:

final_lst = [
    [{'x': 23}, {'y': 23432}, {'z': 78451}, {'a': 564}],
    [{'x': 45}, {'y': 7546}, {'a': 4564}],
    [{'x': 54568}, {'y': 4515}, {'z': 78457}, {'b': 5467}, {'a': 784}]],
]
5 Answers
Related