Sometimes, while working with Python list, we can have a problem in which we need to check if a particular number occurs N consecutive times. This can have application in many domains including day-day programming. Lets discuss certain ways in which this task can be performed.

**Method #1 : Using list comprehension**

This is a way in which this task can be performed. In this, we iterate the list and check for occurrence in list using multiplication operator in a one liner.

`# Python3 code to demonstrate ` `# Test if K occurs N consecutive times ` `# using list comprehension ` ` ` `# Initializing list ` `test_list ` `=` `[` `1` `, ` `3` `, ` `4` `, ` `4` `, ` `4` `, ` `3` `, ` `3` `, ` `2` `, ` `2` `, ` `1` `] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Initializing K ` `K ` `=` `4` ` ` `# Initializing N ` `N ` `=` `3` ` ` `# Test if K occurs N consecutive times ` `# using list comprehension ` `res ` `=` `[K] ` `*` `N ` `in` `(test_list[i : i ` `+` `N] ` `for` `i ` `in` `range` `(` `len` `(test_list) ` `-` `N)) ` ` ` `# printing result ` `print` `(` `"Does K occur N consecutive times ? : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [1, 3, 4, 4, 4, 3, 3, 2, 2, 1] Does K occur N consecutive times ? : True

**Method #2 : Using lambda + join()**

This is yet another way to perform this task. In this, we perform the task of checking for consecution using lambda and join() is used to get the elements groups that are consecutive.

`# Python3 code to demonstrate ` `# Test if K occurs N consecutive times ` `# using lambda + join() ` ` ` `# Initializing list ` `test_list ` `=` `[` `1` `, ` `3` `, ` `4` `, ` `4` `, ` `4` `, ` `3` `, ` `3` `, ` `2` `, ` `2` `, ` `1` `] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Initializing K ` `K ` `=` `4` ` ` `# Initializing N ` `N ` `=` `3` ` ` `# Test if K occurs N consecutive times ` `# using lambda + join() ` `res ` `=` `bool` `(` `lambda` `ele: ` `str` `(K) ` `*` `N ` `in` `''.join(` `str` `(num) ` `for` `num ` `in` `test_list)) ` ` ` `# printing result ` `print` `(` `"Does K occur N consecutive times ? : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [1, 3, 4, 4, 4, 3, 3, 2, 2, 1] Does K occur N consecutive times ? : True

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python | Check if k occurs atleast n times in a list
- Python program to check if given value occurs atleast k times
- Python | Check if any element occurs n times in given list
- Find number of times every day occurs in a Year
- Python - Test if Substring occurs in specific position
- Kolmogorov-Smirnov Test (KS Test)
- Python program to check if the list contains three consecutive common numbers in Python
- Calculate n + nn + nnn + ... + n(m times) in Python
- Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
- Python Program to Find the Number Occurring Odd Number of Times
- Python | Add similar value multiple times in list
- Python | Repeat each element K times in list
- Python | Remove elements of list that are repeated less than k times
- Python | os.times() method
- Python | Repeating tuples N times
- Python | Append K character N times
- Python program to search for the minimum element occurring consecutively n times in a matrix
- Python Tweepy – Getting the number of times a tweet has been retweeted
- Python Tweepy – Getting the number of times a tweet has been favourited
- Python IMDbPY – Getting Series Run times as XML

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.