
- 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 – Extract elements from Ranges in List
When it is required to extract element from ranges in a list, a simple iteration and the ‘extend’ method is used.
Example
Below is a demonstration of the same −
my_list = [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] print("The list is :") print(my_list) range_list = [(12, 14), (17, 18), (22, 28)] print("The list is :") print(range_list) my_result = [] for element in range_list: my_result.extend(my_list[element[0] : element[1] + 1]) print("The result is :") print(my_result)
Output
The list is : [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] The list is : [(12, 14), (17, 18), (22, 28)] The result is : [99, 19]
Explanation
A list of integers is defined and is displayed on the console.
Another list of tuple is defined and displayed on the console.
An empty list is defined.
The list is iterated over, and the zeroth element, and first element incremented 1 is added to the empty list.
This is done using the ‘extend’ method.
This is the output that is displayed on the console.
- Related Articles
- Python – Extract range of Consecutive similar elements ranges from string list
- Python Program to Extract Elements from a List in a Set
- Extract digits from Tuple list Python
- Extract numbers from list of strings in Python
- Python – Extract String elements from Mixed Matrix
- Python program to extract Keywords from a list
- Python – Extract element from a list succeeded by K
- Python program to extract characters in given range from a string list
- Python – Extract tuples with elements in Range
- Python Program that extract words starting with Vowel From A list
- How to extract the names of list elements in R?
- Get last N elements from given list in Python
- Extract tuples having K digit elements in Python
- Extract hyperlinks from PDF in Python
- Python program to extract Mono-digit elements

Advertisements