Python – Extract elements from Ranges in List
Given a list, and a list of tuples with ranges, extract all elements in those ranges from list.
Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 6, 10], range_list = [(2, 4), (7, 8)]
Output : [4, 6, 7, 5, 6]
Explanation : 4, 6, 7 are elements at idx 2, 3, 4 and 5, 6 at idx 7, 8.Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 6, 10], range_list = [(2, 6)]
Output : [4, 6, 7, 5, 4]
Explanation : Elements from 2-6 index are extracted.
Method #1 : Using loop + list slicing
In this, we extract each range using list slicing and using loop iterate for each range and keep extending the extracting slices to extending list.
Python3
# Python3 code to demonstrate working of # Extract elements from Ranges in List# Using loop + list slicing # initializing listtest_list = [4, 5, 4, 6, 7, 5, 4, 5, 4, 6, 4, 6, 9, 8] # printing original listprint("The original list is : " + str(test_list)) # initializing rangesrange_list = [(2, 4), (7, 8), (10, 12)] res = []for ele in range_list: # extending ranges res.extend(test_list[ele[0] : ele[1] + 1]) # printing result print("Ranges elements : " + str(res)) |
The original list is : [4, 5, 4, 6, 7, 5, 4, 5, 4, 6, 4, 6, 9, 8] Ranges elements : [4, 6, 7, 5, 4, 4, 6, 9]
Method #2 : Using list comprehension
In this, we apply similar method as above function, difference being that list comprehension is used to solve this in compact form.
Python3
# Python3 code to demonstrate working of # Extract elements from Ranges in List# Using list comprehensionfrom itertools import chain # initializing listtest_list = [4, 5, 4, 6, 7, 5, 4, 5, 4, 6, 4, 6, 9, 8] # printing original listprint("The original list is : " + str(test_list)) # initializing rangesrange_list = [(2, 4), (7, 8), (10, 12)] # using one-liner to solve this problemres = list(chain.from_iterable([test_list[ele[0] : ele[1] + 1] for ele in range_list])) # printing result print("Ranges elements : " + str(res)) |
The original list is : [4, 5, 4, 6, 7, 5, 4, 5, 4, 6, 4, 6, 9, 8] Ranges elements : [4, 6, 7, 5, 4, 4, 6, 9]





Please Login to comment...