For instance, we have a list like this:
L = ["item1", "item2", "item3", "item3", "item3", "item1", "item2", "item4", "item4", "item4"]
I want to pack them into list of tuples of the form:
[("item1", 1), ("item2", 1), ("item3", 3),... ("item1", 1)]
I've already developed an algorithm which does something similar, to get:
{item1: 2, item2: 2, ...}
(it finds all the occurrences and counts them, even if they aren't neighbours...)
However, I want it to groups only those items which have the same and are neighbours (i.e. occur in a row together), how could I accomplish this?
It's not that I don't know how to do it but I tend to write code that is long and I want an elegant and uncomplicated solution in this case.
item1: 1, item2:1, item3:3....item1: 1
would not be a dictionary... (it has more than one of the same key) – Woo