
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Assign a specific value to Non Max-Min elements in Tuple
When it is required to assign a specific value to the non max-min elements in a tuple, the ‘max’ method, the ‘min’ method, the ‘tuple’ method and a loop can be used.
The ‘max’ method returns the maximum value among all of the elements in an iterable. The ‘min’ method returns the minimum value among all of the elements in an iterable.
The ‘tuple’ method converts a given value/iterable into a tuple type.
Below is a demonstration for the same −
Example
my_tuple = (25, 56, 78, 91, 23, 11, 0, 99, 32, 10) print("The tuple is : ") print(my_tuple) K = 5 print("K has been assigned to " + str(K)) my_result = [] for elem in my_tuple: if elem not in [max(my_tuple), min(my_tuple)]: my_result.append(K) else: my_result.append(elem) my_result = tuple(my_result) print("The tuple after conversion is : " ) print(my_result)
Output
The tuple is : (25, 56, 78, 91, 23, 11, 0, 99, 32, 10) K has been assigned to 5 The tuple after conversion is : (5, 5, 5, 5, 5, 5, 0, 99, 5, 5)
Explanation
- A tuple is defined, and is displayed on the console.
- The value of ‘K’ is defined and displayed.
- An empty list is created.
- The tuple is iterated over, and the maximum and minimum values are determined.
- If these values are not in the tuple, it is appended to the empty list.
- This list is converted to a tuple.
- It is then displayed as output on the console.
- Related Articles
- Sorting max to min value in MySQL
- max() and min() in Python
- Minimum value of “max + min” in a subarray in C++
- Use of min() and max() in Python
- How to define a MIN and MAX value for EditText in Android?
- Program to fill Min-max game tree in Python
- Find Min-Max in heterogeneous list in Python
- Python – Test if elements of list are in Min/Max range from other list
- How to define a MIN and MAX value for EditText in Android using Kotlin?
- Min-Max Heaps
- List Methods in Python - in, not in, len(), min(), max()
- Assign range of elements to List in Python
- Create a vertical slider with custom min, max, and initial value in Java
- How to SELECT min and max value from the part of a table in MySQL?
- How to create a horizontal slider with custom min, max, and initial value in Java

Advertisements