Open In App

Python – Assign K to Non Max-Min elements in Tuple

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python data, we can have a problem in which we need to assign particular value as per certain condition. One of condition can be non-max, min element. This kind of task can occur in many application of day-day programming and competitive programming. Lets discuss certain ways in which this task can be performed.

Input : test_tuple = (3, 6, 3, 6, 34, 34) K = None 
Output : (3, None, 3, None, 34, 34) 

Input : test_tuple = (3, 34) K = None 
Output : (3, 34)

Method #1: Using max() + min() + tuple() + loop The combination of above functions can be used to solve this problem. In this, we perform the task of finding minimum and maximum element using min() and max() and brute force to assign all excepted elements to K. 

Python3




# Python3 code to demonstrate working of
# Assign K to Non Max-Min elements in Tuple
# Using min() + max() + loop + tuple()
 
# initializing tuple
test_tuple = (5, 6, 3, 6, 10, 34)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing K
K = 4
 
# Assign K to Non Max-Min elements in Tuple
# Using min() + max() + loop + tuple()
res = []
for ele in test_tuple:
    if ele not in [max(test_tuple), min(test_tuple)]:
        res.append(K)
    else:
        res.append(ele)
res = tuple(res)
 
# printing result
print("The tuple after conversion: " + str(res))


Output : 

The original tuple : (5, 6, 3, 6, 10, 34)
The tuple after conversion: (4, 4, 3, 4, 4, 34)

Time complexity: O(n), where n is the length of the input tuple test_tuple.

Auxiliary space: O(n). The program creates a list called res with n elements, which is used to store the modified tuple elements before converting it back to a tuple. 

Method #2: Using generator expression + max() + min() + tuple() This is one liner approach by which this task can be performed. In this, we extract all the elements and assign with appropriate condition using generator expression. 

Python3




# Python3 code to demonstrate working of
# Assign K to Non Max-Min elements in Tuple
# Using generator expression + max() + min() + tuple()
 
# initializing tuple
test_tuple = (5, 6, 3, 6, 10, 34)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing K
K = 4
 
# Assign K to Non Max-Min elements in Tuple
# Using generator expression + max() + min() + tuple()
res = tuple(ele if ele in [min(test_tuple), max(test_tuple)] else K for ele in test_tuple)
 
# printing result
print("The tuple after conversion: " + str(res))


Output : 

The original tuple : (5, 6, 3, 6, 10, 34)
The tuple after conversion: (4, 4, 3, 4, 4, 34)

Time complexity: O(n), where n is the length of the tuple, because we need to iterate over all elements in the tuple once.
Auxiliary space: O(n) because we are creating a new tuple to store the result. 

Method #3: Using list comprehension + sorted() + slicing + tuple()

Uses list comprehension to iterate over the elements of the original tuple and replaces any element that is not equal to the minimum or maximum element with the value of K. The minimum and maximum elements are obtained by first sorting the tuple using the sorted() function and then accessing the first and last elements of the sorted tuple using slicing. The final result is converted back to a tuple using the tuple() function.

Python3




# Python3 code to demonstrate working of
# Assign K to Non Max-Min elements in Tuple
# Using list comprehension + sorted() + slicing + tuple()
 
# initializing tuple
test_tuple = (5, 6, 3, 6, 10, 34)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing K
K = 4
 
# Assign K to Non Max-Min elements in Tuple
# Using list comprehension + sorted() + slicing + tuple()
sorted_tuple = sorted(test_tuple)
res = tuple([K if ele not in [sorted_tuple[0], sorted_tuple[-1]] else ele for ele in test_tuple])
 
# printing result
print("The tuple after conversion: " + str(res))


Output

The original tuple : (5, 6, 3, 6, 10, 34)
The tuple after conversion: (4, 4, 3, 4, 4, 34)

Time complexity: O(n log n), where n is the length of the input tuple.
Auxiliary space: O(n), where n is the length of the input tuple.

Method 4: Using enumerate() + conditional statement + sorted() + tuple()

This method uses the enumerate() function to iterate over the tuple and check if the element is not the maximum or minimum value in the sorted tuple, and then assign the value K to it.

Step-by-step approach:

  • Initialize the tuple and print the original tuple.
  • Initialize the value K.
  • Sort the tuple in ascending order using the sorted() function.
  • Create an empty list to store the new values.
  • Use the enumerate() function to iterate over the tuple.
  • Check if the element is not equal to the minimum or maximum value in the sorted tuple.
  • If the element is not equal to the minimum or maximum value, then assign the value K to it and append it to the list.
  • If the element is equal to the minimum or maximum value, then append the element to the list as it is.
  • Convert the list to a tuple using the tuple() function.
  • Print the new tuple.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Assign K to Non Max-Min elements in Tuple
# Using enumerate() + conditional statement + sorted() + tuple()
 
# initializing tuple
test_tuple = (5, 6, 3, 6, 10, 34)
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing K
K = 4
 
# Assign K to Non Max-Min elements in Tuple
# Using enumerate() + conditional statement + sorted() + tuple()
sorted_tuple = sorted(test_tuple)
new_list = []
for idx, ele in enumerate(test_tuple):
    if ele != sorted_tuple[0] and ele != sorted_tuple[-1]:
        new_list.append(K)
    else:
        new_list.append(ele)
res = tuple(new_list)
 
# printing result
print("The tuple after conversion: " + str(res))


Output

The original tuple : (5, 6, 3, 6, 10, 34)
The tuple after conversion: (4, 4, 3, 4, 4, 34)

Time complexity: O(n log n), due to the sorting operation
Auxiliary space: O(n), as we are using a list to store the new values before converting it into a tuple.

Method 5: Using list() + for loop + if-else statement + tuple()

Step-by-step approach:

  1. Initialize the tuple.
  2. Print the original tuple.
  3. Initialize the value of K.
  4. Create an empty list to store the modified elements.
  5. Use a for loop to iterate through the elements of the tuple.
  6. Check if the element is equal to the minimum or maximum element of the tuple. If it is, append it to the modified list.
  7. Otherwise, append K to the modified list.
  8. Convert the modified list to a tuple.
  9. Print the result.

Python3




# Initializing tuple
test_tuple = (5, 6, 3, 6, 10, 34)
 
# Printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Initializing K
K = 4
 
# Creating an empty list to store the modified elements
modified_list = []
 
# Iterating through the elements of the tuple
for ele in test_tuple:
    # Checking if the element is equal to the minimum or maximum element of the tuple
    if ele == min(test_tuple) or ele == max(test_tuple):
        # Appending the element to the modified list
        modified_list.append(ele)
    else:
        # Appending K to the modified list
        modified_list.append(K)
 
# Converting the modified list to a tuple
res = tuple(modified_list)
 
# Printing the result
print("The tuple after conversion: " + str(res))


Output

The original tuple : (5, 6, 3, 6, 10, 34)
The tuple after conversion: (4, 4, 3, 4, 4, 34)

Time complexity: O(n)
Auxiliary space: O(n) (to store the modified list)



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads