- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Trim tuples by K
When it is required to trim tuples based on a K value, a simple iteration and the ‘append’ method is used.
Below is a demonstration of the same −
my_list = [(44, 3, 68, 11, 5), (68, 44, 9, 5, 8), (8, 11, 2, 68, 5), (44, 68, 2, 5, 7)] print("The list is :") print(my_list) K = 1 print("The value for K is ") print(K) my_result =  for element in my_list: list_length = len(element) my_result.append(tuple(list(element)[K: list_length - K])) print("The resultant list is :") print(my_result)
The list is : [(44, 3, 68, 11, 5), (68, 44, 9, 5, 8), (8, 11, 2, 68, 5), (44, 68, 2, 5, 7)] The value for K is 1 The resultant list is : [(3, 68, 11), (44, 9, 5), (11, 2, 68), (68, 2, 5)]
A list of tuple is defined and is displayed on the console.
The value for K is defined and is displayed on the console.
An empty list is defined.
The list is iterated over and the length of every element is stored in a variable.
The elements from K to difference between length of list and K are accessed using indexing, and converted to a tuple.
This is appended to the empty list.
This is displayed as output on the console.
- Trim tuples by N elements in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Remove Tuples of Length K in Python
- Extract tuples having K digit elements in Python
- Python – Filter Tuples Product greater than K
- Python – Remove Tuples with difference greater than K
- Sort Tuples by Total digits in Python
- Python – Sort Tuples by Total digits
- List expansion by K in Python
- Python program to Sort Tuples by their Maximum element
- Filter Tuples by Kth element from List in Python
- Sort list of tuples by specific ordering in Python
- Find top K frequent elements from a list of tuples in Python
- Smallest Integer Divisible by K in Python
- Python – Sort row by K multiples