Three way partitioning of an array around a given range - Python
Last Updated :
18 Jan, 2025
When working with arrays or lists in Python, sometimes we need to partition the elements into three separate groups based on a given range. The goal is to rearrange the elements such that:
- Elements smaller than the lower bound of the range come first.
- Elements within the range come next.
- Elements greater than the upper bound of the range appear last.
Let's explore various methods to achieve this using Python.
Using List Comprehension
We can efficiently use list comprehension to create three separate partitions in a single step.
Python
# Input array and range
arr = [12, 5, 18, 7, 30, 15, 2, 9]
low, high = 10, 20
# Three-way partitioning using list comprehension
res = [x for x in arr if x < low] + [x for x in arr if low <= x <= high] + [x for x in arr if x > high]
print(res)
Output[5, 7, 2, 9, 12, 18, 15, 30]
Explanation:
- We use three list comprehensions to filter elements less than
low
, within the range [low, high]
and greater than high
. - final result is obtained by concatenating the three partitions.
Let's explore some more methods and see how we can do three way partitioning of an array around a given range.
Using filter()
We can also use the function filter() function to create three separate partitions and concatenate them.
Python
# Input array and range
arr = [12, 5, 18, 7, 30, 15, 2, 9]
low, high = 10, 20
# Create partitions using filter
less = list(filter(lambda x: x < low, arr))
within = list(filter(lambda x: low <= x <= high, arr))
greater = list(filter(lambda x: x > high, arr))
# Combine the partitions
res = less + within + greater
print(res)
Output[5, 7, 2, 9, 12, 18, 15, 30]
Explanation:
- We use filter() with lambda functions to extract elements for each partition.
- Finally, we concatenate the partitions to get the desired result.
Using For Loop
This method uses a for loop to traverse the array and append elements into three separate lists based on the range.
Python
# Input array and range
arr = [12, 5, 18, 7, 30, 15, 2, 9]
low, high = 10, 20
# Initialize lists for three partitions
less, within, greater = [], [], []
# Traverse and partition elements
for x in arr:
if x < low:
less.append(x)
elif low <= x <= high:
within.append(x)
else:
greater.append(x)
# Combine the partitions
res = less + within + greater
print(res)
Output[5, 7, 2, 9, 12, 18, 15, 30]
Explanation:
- We iterate through the array and append elements to
less
, within
or greater
based on their values. - Finally, we combine the three lists to form the partitioned array.
Using a Single Loop with In-place Rearrangement
This method modifies the array in-place without using additional space for separate lists.
Python
# Input array and range
arr = [12, 5, 18, 7, 30, 15, 2, 9]
low, high = 10, 20
# Initialize pointers
start, mid, end = 0, 0, len(arr) - 1
# Partition the array in place
while mid <= end:
if arr[mid] < low:
arr[start], arr[mid] = arr[mid], arr[start]
start += 1
mid += 1
elif low <= arr[mid] <= high:
mid += 1
else:
arr[mid], arr[end] = arr[end], arr[mid]
end -= 1
print(arr)
Output[5, 7, 9, 2, 18, 15, 12, 30]
Explanation:
- The method uses three pointers to rearrange the array in one traversal.
- start trackselements less than
low
, mid
tracks elements within the range, and end
tracks elements greater than high
.
Similar Reads
Python - Numbers in a list within a given range We are given a list and we are given a range we need to count how many number lies in the given range. For example, we are having a list n = [5, 15, 25, 35, 45, 55, 65, 75] and we are having range lower=20, upper=60 so we need to count how many element lies between this range so that the output shou
4 min read
Numpy recarray.partition() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array
3 min read
SymPy | Partition.as_dict() in Python Partition.as_dict() : as_dict() is a sympy Python library function that returns the partition as a dictionary. In this dictionary, the keys are the partition integers. The values are the multiplicity of that integer. Syntax : sympy.combinatorics.partitions.Partition.as_dict() Return : Partition as a
1 min read
Remove All Sublists Outside the Given Range - Python The problem "Remove All Sublists Outside the Given Range" involves filtering out sublists from a list of lists based on a specified range. Each sublist is assessed and only those where all elements fall within the given range (e.g., between low and high) are kept.We are given a matrix m= m = [[1, 2,
3 min read
SymPy | Partition.conjugate() in Python Partition.conjugate() : conjugate() is a sympy Python library function that returns the conjugate partition of the argumented partition. Syntax : sympy.combinatorics.partitions.Partition.conjugate() Return : conjugate partition Code #1 : conjugate() ExamplePython3 1=1 # Python code explaining # SymP
3 min read