- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python - Consecutive Ranges of K greater than N
When it is required to get the consecutive ranges of ‘K’ which are greater than ‘N’, the ‘enumerate’ attribute and simple iteration is used.
Below is a demonstration of the same
my_list = [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65] print("The list is :") print(my_list) K = 65 N = 3 print("The value of K is ") print(K) print("The value of N is ") print(N) my_result =  beg, end = 0, 0 previous = 1 for index, element in enumerate(my_list): if element == K: end = index if previous != K: beg = index else: if previous == K and end - beg + 1 >= N: my_result.append((beg, end)) previous = element print("The result is :") print(my_result)
The list is : [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65] The value of K is 65 The value of N is 3 The result is : [(4, 10)]
A list is defined and is displayed on the console.
The values for ‘K’ and ‘N’ are defined and are displayed on the console.
An empty list is defined.
The value for ‘previous’ is defined.
The values for ‘beginning’ and ‘end’ are defined.
The list is iterated over by enumerating it.
If any element in the list is equivalent to another value ‘k’, the index value is redefined.
Otherwise, the values of ‘previous’ is redefined.
The beginning and end values are appended to the empty list.
This is returned as output.
The output is displayed on the console.
- Related Articles
- Python Indices of numbers greater than K
- Python – Average of digit greater than K
- Python – Remove characters greater than K
- Python – Filter Tuples Product greater than K
- Python - Number of values greater than K in list
- Find smallest element greater than K in Python
- Python – Remove Tuples with difference greater than K
- Python - Get the Index of first element greater than K
- Python – Extract dictionaries with values sum greater than K
- Python – Extract range of Consecutive similar elements ranges from string list
- Python – Extract list with difference in extreme values greater than K
- Remove tuples from list of tuples if greater than n in Python
- Find k-th smallest element in given n ranges in C++
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Count of alphabets having ASCII value less than and greater than k in C++