Python - Extract Elements from Ranges in List
Last Updated :
10 Feb, 2025
We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]].
Using List Comprehension
List comprehension allows us to extract elements from a list that fall within a specified range using a concise one-liner. It iterates through the list applying a conditional check to include only matching elements.
Python
n = [10, 20, 30, 40, 50, 60, 70, 80, 90]
r = [(1, 3), (5, 7)]
# Extract sublists using list comprehension
res = [n[i:j+1] for i, j in r]
print(res)
Output[[20, 30, 40], [60, 70, 80]]
Explanation:
- List comprehension iterates over the list of tuples r extracting sublists from n using slicing (n[i:j+1]) for each (i, j) range.
- Resulting list res contains extracted sublists which are then printed.
Using itertools.chain
itertools.chain allows us to flatten multiple extracted sublists into a single iterable. By applying chain to sliced sublists we can directly obtain elements from specified index ranges in a list.
Python
from itertools import chain
n = [10, 20, 30, 40, 50, 60, 70, 80, 90]
r = [(1, 3), (5, 7)]
# Extract sublists using list comprehension and flatten them using chain.from_iterable
res = list(chain.from_iterable(n[i:j+1] for i, j in r))
print(res)
Output[20, 30, 40, 60, 70, 80]
Explanation:
- List comprehension extracts sublists from n based on index ranges in r creating multiple smaller lists.
- chain.from_iterable() flattens these sublists into a single list ensuring all extracted elements are combined into res.
Using sum()
Using sum()
with list comprehension helps extract and flatten elements from specified index ranges in a list. By setting an empty list []
as starting value sum()
merges multiple sublists into a single list.
Python
n = [10, 20, 30, 40, 50, 60, 70, 80, 90]
r = [(1, 3), (5, 7)]
# Extract sublists using list comprehension and flatten them using sum()
res = sum((n[i:j+1] for i, j in r), [])
print(res)
Output[20, 30, 40, 60, 70, 80]
Explanation:
- List comprehension extracts sublists from n based on the index ranges in r creating separate sublists.
- sum([...], []) flattens these sublists by adding them to an empty list resulting in a single merged list of extracted elements.
Using a Loop
Using a for loop we can iterate over a list of index ranges and extract elements from main list using slicing extracted sublists can then be extended into a single list to achieve a flattened result.
Python
n = [10, 20, 30, 40, 50, 60, 70, 80, 90]
r = [(1, 3), (5, 7)]
res = []
for start, end in r:
res.extend(n[start:end+1])
print(res)
Output[20, 30, 40, 60, 70, 80]
Explanation:
- for loop iterates through list of index range tuples r slicing n[start:end+1] to extract sublists.
- res.extend() flattens the extracted sublists by adding their elements directly into res producing a single merged list.
Similar Reads
Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read
Python - Extract records if Kth elements not in List Given list of tuples, task is to extract all the tuples where Kth index elements are not present in argument list. Input : test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)], arg_list = [6, 8, 8], K = 1 Output : [(5, 3), (7, 4), (1, 3)] Explanation : All the elements which have either 6 or 8 at 1s
4 min read
Python - Extract element from list succeeded by K Given a list, extract the elements which are having K as the next element. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5], K = 3 Output : [2, 5] Explanation : Elements before 3 are 2, 5. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 8], K = 8 Output : [7, 3] Explanation : Elements before 8 are 7, 3. Metho
5 min read
Python - Extract tuples with elements in Range Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read