- 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 in between multiple specific range of index
When it is required to extract elements that are in between multiple specific range of index, the ‘extend’ method, a simple iteration and indexing are used.
Below is a demonstration of the same −
my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] print("The list is : ") print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) range_list = [(2, 4), (7, 8), (1, 2), (2,7)] my_result =  for element in range_list: my_result.extend(my_list[element : element + 1]) print("The resultant list is : ") print(my_result) print("The result after sorting is : " ) my_result.sort() print(my_result)
The list is : [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] The list after sorting is : [0, 10, 11, 13, 13, 17, 18, 21, 22, 81, 90] The resultant list is : [11, 13, 13, 21, 22, 10, 11, 11, 13, 13, 17, 18, 21] The result after sorting is : [10, 11, 11, 11, 13, 13, 13, 13, 17, 18, 21, 21, 22]
A list is defined and is displayed on the console.
It is sorted and displayed on the console.
Another list of tuples is defined. It indicates the ranges.
An empty list is defined.
The list is iterated over, and using list indexing the current and next element incremented by 1 are appended to the empty list.
This is displayed as output on the console.
It is sorted and displayed again on the console.
- Python – Extract tuples with elements in Range
- Python - Make new Pandas Index with deleting multiple index elements
- Python – Extract range of Consecutive similar elements ranges from string list
- Write a program in Python to print the elements in a series between a specific range
- Python - Index Directory of Elements
- Python - Index Ranks of Elements
- Python – Check if elements in a specific index are equal for list elements
- Python Extract specific keys from dictionary?
- Swap Even Index Elements And Odd Index Elements in Python
- Assign range of elements to List in Python
- Delete elements in range in Python
- Why substring slicing index out of range works in Python?
- Python Pandas - Repeat elements of an Index
- Python Index specific cyclic iteration in list
- Python – Grouped Consecutive Range Indices of Elements