How to find a duplicate of a list that repeats three times in a row

3964 views python
10

Given a list of duplicate numbers, how to find a number that repeats three times in a row. For example:

list = [1,1,2,2,3,3,3,4,4]

I want to print element number 3 as it appears three times in a row.

I have tried using Counter, which converts this to dict, but not sure how to just print the key that as the max count value.

answered question

Side note: never [even in an example] name a variable after a built-in, e.g. use L or lst instead of list.

1 Answer

3

Using a list comprehension with zip:

L = [1,1,2,2,3,3,3,4,4]

res = [i for i, j, k in zip(L, L[1:], L[2:]) if i == j == k]  # [3]

posted this

Have an answer?

JD

Please login first before posting an answer.