Note that the original question was "compare consecutive pairs and then let only the elements with the same p[0]" which means that one should not sort the original list. If the original list is sorted, then pairs which would otherwise not be "consecutive" will be picked up and kept. While the sample list was sorted, I will not assume that to be the case so that this will also handle a case such as
[[13, 5],
[13, 2],
[14, 9],
[13. 6]]
to generate
[[13, 5],
[13, 2]]
I will show a simple loop in order to make it easier to understand, but list comprehension can make it shorter. Note that Counter requires python 2.7
prev = None
newp = []
length = len(p) - 1
for i in range(length):
if p[i][0] == p[i+1][0] or p[i][0] == prev:
newp.append(p[i])
prev = p[i][0]
if p[length][0] == p[length-1][0]:
newp.append(p[length])
This will create the new list as you want