What you wrote is O(n^2). The version I wrote is O(n).
Counting all elements (even of a sublist) every time through the loop is inefficient and pushes the algorithm to quadratic time.
Also note you could just use
Counting all elements (even of a sublist) every time through the loop is inefficient and pushes the algorithm to quadratic time.
Also note you could just use
list.count
(or tuple.count
depending) for your method:sequence = 48,52,55,48,52,55,60,62,48 result = [sequence[:i].count(val) for i,val in enumerate(sequence, 1)]but this is still O(n^2) which should be avoided.