- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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 : element + 1]) print("The result is :") print(my_result)
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]
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.
- 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 that extract words starting with Vowel From A list
- Python program to extract characters in given range from a string list
- Extract hyperlinks from PDF in Python
- How to extract the names of list elements in R?
- Get last N elements from given list in Python
- Python program to extract Mono-digit elements
- Extract tuples having K digit elements in Python
- Python – Extract tuples with elements in Range