Sometimes while working with data, we can have a problem in which we need to select some of the elements that occur K times consecutively. This problem can occur in many domains. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using groupby() + list comprehension
This task can be performed using above functionalities. In this, we group all the numbers that are occurring K consecutively. We iterate the list using list comprehension.
# Python3 code to demonstrate working of # Retain K consecutive elements # using groupby() + list comprehension from itertools import groupby # initialize list test_list = [1, 1, 4, 5, 5, 6, 7, 7, 8] # printing original list print("The original list : " + str(test_list)) # initialize K K = 2 # Retain K consecutive elements # using groupby() + list comprehension res = [i for i, j in groupby(test_list) if len(list(j)) == K] # printing result print("The K consecutive elements are : " + str(res)) |
The original list : [1, 1, 4, 5, 5, 6, 7, 7, 8] The K consecutive elements are : [1, 5, 7]
Method #2 : Using list comprehension + slice() + groupby()
This task can also be performed using above functions. In this, we just perform grouping in similar way as above but the way we extract consecutive elements is by slice().
# Python3 code to demonstrate working of # Retain K consecutive elements # using groupby() + list comprehension + islice() from itertools import groupby, islice # initialize list test_list = [1, 1, 4, 5, 5, 6, 7, 7, 8] # printing original list print("The original list : " + str(test_list)) # initialize K K = 2 # Retain K consecutive elements # using groupby() + list comprehension + islice() res = [i for i, j in groupby(test_list) if len(list(islice(j, 0, K))) == K] # printing result print("The K consecutive elements are : " + str(res)) |
The original list : [1, 1, 4, 5, 5, 6, 7, 7, 8] The K consecutive elements are : [1, 5, 7]
Recommended Posts:
- Python | Retain K Front and Rear elements
- Python - Retain list elements value items
- Python - Retain all K elements Rows
- Python - Retain first N Elements of a String and Replace the Remaining by K
- Python | Retain records of specific length
- Python - Retain K match index values from other list
- Python - Retain records with N occurrences of K
- Python - Retain Numbers in String
- Python | Consecutive elements pairing in list
- Python | Group consecutive list elements with tolerance
- Python | Consecutive elements grouping in list
- Python | Consecutive remaining elements in list
- Python | Consecutive duplicates all elements deletion in list
- Python | Concatenate N consecutive elements in String list
- Python - Multiply Consecutive elements in list
- Python - Concatenate consecutive elements in Tuple
- Python - Extract elements with Range consecutive occurrences
- Python - Consecutive Missing elements Sum
- Python | Consecutive Custom Chunked elements Product
- Python - Similar Consecutive elements frequency
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.

