Let’s say I have a list of strings:
a = ['a', 'a', 'b', 'c', 'c', 'c', 'd']
I want to make a list of items that appear at least twice in a row:
result = ['a', 'c']
I know I have to use a for loop, but I can’t figure out how to target the items repeated in a row.
How can I do so?
EDIT: What if the same item repeats twice in a? Then the set function would be ineffective
a = ['a', 'b', 'a', 'a', 'c', 'a', 'a', 'a', 'd', 'd']
result = ['a', 'a', 'd']
try
itertools.groupby()here:using
islice():using
zip()andizip():timeitresults:output:
Conclusion:
Poke’s solution is the fastest solution in comparison to other alternatives.