Python finding n consecutive numbers in a list

Posted by lost_in_code on Stack Overflow See other posts from Stack Overflow or by lost_in_code
Published on 2010-05-28T07:07:55Z Indexed on 2010/05/28 7:11 UTC
Read the original article Hit count: 137

Filed under:

I want to know how to find if there is a certain amount of consecutive numbers in a row in my list e.g.

For example if I am looking for two 1's then:

list = [1, 1, 1, 4, 6] #original list
list = ["true", "true", 1, 4, 6] #after my function has been through the list.

If I am looking for three 1's then:

list = [1, 1, 1, 4, 6] #original list
list = ["true", "true", "true", 4, 6] #after my function has been through the list.

I have tried:

list = [1, 1, 2, 1]

1,1,1 in list #typed into shell, returns "(1, 1, True)"

Any help would be greatly appreciated, I mainly would like to understand whats going on, and how to check if the next element in the list is the same as the first x amount.

© Stack Overflow or respective owner

Related posts about python