- 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
Remove Tuples of Length K in Python
When it is required to remove the tuples of a specific length ‘K’, list comprehension can be used.
Below is the demonstration of the same −
my_list = [(32, 51), (22,13 ), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is : ") print(my_result)
The list is : [(32, 51), (22, 13), (94, 65, 77), (70,), (80, 61, 13, 17)] The value of K is 1 The filtered list is : [(32, 51), (22, 13), (94, 65, 77), (80, 61, 13, 17)]
A list of tuple is defined, and is displayed on the console.
The value of K is assigned and displayed on the console.
The list comprehension is used to check the length of every element in the list of tuple.
This is assigned to a variable
It is displayed as output on the console.
- Python – Remove Tuples with difference greater than K
- Remove duplicate tuples from list of tuples in Python
- Remove matching tuples in Python
- Remove tuples from list of tuples if greater than n in Python
- Python – Trim tuples by K
- Remove tuples having duplicate first value from given list of tuples in Python
- Extract tuples having K digit elements in Python
- Program to find maximum length of k ribbons of same length in Python
- Python | Remove empty tuples from a list
- C++ Remove Nodes on Root to Leaf Paths of Length < K
- Python – Filter Tuples Product greater than K
- Remove duplicate lists in tuples (Preserving Order) in Python
- Find top K frequent elements from a list of tuples in Python
- Combining tuples in list of tuples in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples