Open In App

Python - Maximum and Minimum K elements in Tuple

Last Updated : 29 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to extract the K largest and K smallest elements from a tuple in Python using several methods.

For Example:

Input:
t = (2, 4, 3, 6, 8, 11, 1, 9)
k=2

Output:
min = 1, 2
max = 9, 11

Here, the two smallest elements in the tuple 't' are 1 and 2, while the two largest elements are 9 and 11."

Method 1: Using heapq Module

The heapq module allows efficient selection of the K smallest and largest elements.

Python
import heapq

tup = (5, 20, 3, 7, 6, 8)
K = 2

smallest = heapq.nsmallest(K, tup)
largest = heapq.nlargest(K,tup)

print("K minimum elements:", smallest)
print("K maximum elements:", largest)

Output
K minimum elements: [3, 5]
K maximum elements: [20, 8]

Explanation:

  • heapq.nsmallest(K, iterable): Returns the K smallest elements from the iterable.
  • heapq.nlargest(K, iterable): Returns the K largest elements from the iterable.

Method 2: Using List Slicing + sorted()

The combination of above functions can be used to solve this problem. In this, we perform the task of max, min extraction using slicing rather than brute force loop logic.

Python
tup = (5, 20, 3, 7, 6, 8)
K = 2

temp = sorted(list(tup))

min_ele = temp[:K]
max_ele = temp[-K:]

print("K minimum elements:", min_ele)
print("K maximum elements:", max_ele)

Output
K minimum elements: [3, 5]
K maximum elements: [8, 20]

Explanation:

  • list(test_tup): Converts the tuple into a list for sorting.
  • sorted(list): Sorts the list in ascending order.
  • temp[:K]: Slices the first K elements as the K minimum elements.
  • temp[-K:]: Slices the last K elements as the K maximum elements.

Method 3: Using sorted() + loop

Sort the tuple and use a loop to extract the first K smallest and last K largest elements.

Python
tup = (5, 20, 3, 7, 6, 8)
K = 2

sorted_list = sorted(tup)

# Use a loop to select first K (min) and last K (max) elements
min_ele = []
max_ele = []

for idx, val in enumerate(sorted_list):
    if idx < K:
        min_ele.append(val)
    if idx >= len(sorted_list) - K:
        max_ele.append(val)

print("K minimum elements:", min_ele)
print("K maximum elements:", max_ele)

Output
K minimum elements: [3, 5]
K maximum elements: [8, 20]

Explanation:

  • sorted(tup): Sorts the tuple in ascending order.
  • enumerate(sorted_list): Loops over the sorted list with index and value.
  • if idx < K: Appends first K elements to min_ele.
  • if idx >= len(sorted_list) - K: Appends last K elements to max_ele.

Method 4: Using min() and max() in a Loop

Iteratively track K smallest and K largest elements by comparing each tuple item with the current min and max lists.

Python
tup = (5, 20, 3, 7, 6, 8)
K = 2

min_list = []
max_list = []

for elem in tup:
    # K smallest
    if len(min_list) < K:
        min_list.append(elem)
    else:
        if elem < max(min_list):
            min_list.remove(max(min_list))
            min_list.append(elem)
    
    # K largest
    if len(max_list) < K:
        max_list.append(elem)
    else:
        if elem > min(max_list):
            max_list.remove(min(max_list))
            max_list.append(elem)

min_list.sort()
max_list.sort()

print("K minimum elements:", min_list)
print("K maximum elements:", max_list)

Output
K minimum elements: [3, 5]
K maximum elements: [8, 20]

Explanation:

  • Use min_list and max_list to track K smallest and largest elements.
  • Replace largest in min_list if smaller element found.
  • Replace smallest in max_list if larger element found.
  • Sort lists before printing.

Method 5: Using a loop and two lists

This method keeps track of the K smallest and K largest elements simultaneously in two separate lists while iterating through the tuple.

Python
tup = (5, 20, 3, 7, 6, 8)
K = 2

min_list = []
max_list = []

for elem in tup:
    # Maintain K smallest elements
    if len(min_list) < K:
        min_list.append(elem)
    else:
        if elem < max(min_list):
            min_list.remove(max(min_list))
            min_list.append(elem)
    
    # Maintain K largest elements
    if len(max_list) < K:
        max_list.append(elem)
    else:
        if elem > min(max_list):
            max_list.remove(min(max_list))
            max_list.append(elem)


min_list.sort()          
max_list.sort(reverse=True) 

print("K minimum elements:", min_list)
print("K maximum elements:", max_list)

Output
K minimum elements: [3, 5]
K maximum elements: [20, 8]

Explanation:

  • Track K smallest in min_list and K largest in max_list.
  • Replace largest in min_list if smaller element found.
  • Replace smallest in max_list if larger element found.

Method 6: Using while loop + min() / max()

This method repeatedly finds the minimum and maximum values using a while loop and appends them to separate lists.

Python
tup = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
K = 2

tl = list(tup)

min_list = []
max_list = []

i = 0

while i < K:
    min_val = min(tl)
    min_list.append(min_val)
    tl.remove(min_val)
    i += 1

i = 0

while i < K:
    max_val = max(tl)
    max_list.append(max_val)
    tl.remove(max_val)
    i += 1

min_list.sort()
max_list.sort(reverse=True)

print("K minimum elements:", min_list)
print("K maximum elements:", max_list)

Output
K minimum elements: [0, 1]
K maximum elements: [9, 8]

Explanation:

  • list(tup): Convert tuple to list for mutability.
  • Use while loop to extract K minimum elements using min().
  • Remove extracted minimums from the list.
  • Use while loop to extract K maximum elements using max().
  • Remove extracted maximums from the list.

Explore