- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum and Minimum K elements in Tuple using Python
When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.
Below is the demonstration of the same −
my_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result =  my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp): if idx < K or idx >= len(temp) - K: my_result.append(val) my_result = tuple(my_result) print("The result is : " ) print(my_result)
The tuple is : (7, 25, 36, 9, 6, 8) The value of K has been initialized to 2 The result is : (6, 7, 25, 36)
A tuple is defined, and is displayed on the console.
The value of K is defined.
An empty list is defined.
The tuple is converted to a list.
It is sorted and stored in a variable.
This is iterated over, and if it is less than K or greater than difference between length of list and K, it is appended to the empty list.
This is the output that is displayed on the console.
- Find minimum k records from tuple list in Python
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Program to find minimum possible maximum value after k operations in python
- Program to find minimum amplitude after deleting K elements in Python
- Delete Tuple Elements in Python
- Test if Tuple contains K in Python
- Maximum element in tuple list in Python
- Get minimum difference in Tuple pair in Python
- Modulo of tuple elements in Python
- Find minimum and maximum elements in singly Circular Linked List in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Recursive Programs to find Minimum and Maximum elements of array in C++
- Minimum number of elements to be removed to make XOR maximum using C++.
- Find Maximum difference between tuple pairs in Python