Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python – Extract elements in between multiple specific range of index
When you need to extract elements from multiple specific index ranges in a list, you can use the extend() method with list slicing and iteration. This technique allows you to collect elements from different sections of a list based on defined ranges.
Basic Example
Here's how to extract elements from multiple index ranges ?
my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0]
print("Original list:")
print(my_list)
# Define multiple ranges as (start, end) tuples
range_list = [(2, 4), (7, 8), (1, 2)]
# Extract elements from specified ranges
result = []
for start, end in range_list:
result.extend(my_list[start:end + 1])
print("Elements from ranges", range_list)
print(result)
Original list: [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] Elements from ranges [(2, 4), (7, 8), (1, 2)] [81, 10, 13, 18, 11, 21, 81]
Working with Sorted Data
You can also work with sorted data for consistent ordering ?
my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0]
print("Original list:")
print(my_list)
# Sort the list first
my_list.sort()
print("Sorted list:")
print(my_list)
# Define ranges
range_list = [(2, 4), (7, 8), (1, 2)]
# Extract elements from ranges
result = []
for start, end in range_list:
result.extend(my_list[start:end + 1])
print("Extracted elements:")
print(result)
print("Sorted result:")
result.sort()
print(result)
Original list: [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] Sorted list: [0, 10, 11, 13, 13, 17, 18, 21, 22, 81, 90] Extracted elements: [11, 13, 13, 21, 22, 10, 11, 11, 13, 13, 17, 18, 21] Sorted result: [10, 11, 11, 11, 13, 13, 13, 13, 17, 18, 21, 21, 22]
Alternative Approach Using List Comprehension
You can achieve the same result using list comprehension for a more concise solution ?
my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0]
range_list = [(2, 4), (7, 8), (1, 2)]
# Using list comprehension with nested loops
result = [item for start, end in range_list for item in my_list[start:end + 1]]
print("Original list:", my_list)
print("Ranges:", range_list)
print("Extracted elements:", result)
Original list: [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] Ranges: [(2, 4), (7, 8), (1, 2)] Extracted elements: [81, 10, 13, 18, 11, 21, 81]
How It Works
The process involves these key steps:
-
Range Definition: Each tuple
(start, end)defines a range where both indices are inclusive -
List Slicing:
my_list[start:end + 1]extracts elements from start to end (inclusive) -
Extend Method:
extend()adds all elements from the slice to the result list - Iteration: The loop processes each range sequentially
Conclusion
Use extend() with list slicing to extract elements from multiple index ranges. List comprehension provides a more concise alternative for the same functionality.
Advertisements
