I am presented with a list made entirely of tuples, such as:
lst = [("hello", "Blue"), ("hi", "Red"), ("hey", "Blue"), ("yo", "Green")]
How can I split lst
into as many lists as there are colours? In this case, 3 lists
[("hello", "Blue"), ("hey", "Blue")]
[("hi", "Red")]
[("yo", "Green")]
I just need to be able to work with these lists later, so I don't want to just output them to screen.
Details about the list
I know that every element of lst
is strictly a double-element tuple. The colour is also always going to be that second element of each tuple.
The problem
Problem is,lst
is dependant on user input, so I won't always know how many colours there are in total and what they are. That is why I couldn't predefine variables to store these lists in them.
So how can this be done?