Open In App

Python – Extract Elements from Ranges in List

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Practice Tags :

Similar Reads