Python - Filter tuple with all same elements
Last Updated :
05 May, 2023
Given List of tuples, filter tuples that have same values.
Input : test_list = [(5, 6, 5, 5), (6, 6, 6), (9, 10)]
Output : [(6, 6, 6)]
Explanation : 1 tuple with same elements.
Input : test_list = [(5, 6, 5, 5), (6, 5, 6), (9, 10)]
Output : []
Explanation : No tuple with same elements.
Method #1 : Using list comprehension + set() + len()
In this, we check for length of set converted tuple to be 1, if that checks out, tuple is added to result, else, omitted.
Python3
# Python3 code to demonstrate working of
# Filter similar elements Tuples
# Using list comprehension + set() + len()
# initializing list
test_list = [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
# printing original list
print("The original list is : " + str(test_list))
# length is computed using len()
res = [sub for sub in test_list if len(set(sub)) == 1]
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
Filtered Tuples : [(6, 6, 6), (1, 1)]
Time complexity: O(n), where n is the length of the test_list. The list comprehension + set() + len() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using filter() + lambda + set() + len()
In this, we perform task of filtering using filter(), and single element logic is checked in lambda function using set() and len().
Python3
# Python3 code to demonstrate working of
# Filter similar elements Tuples
# Using filter() + lambda + set() + len()
# initializing list
test_list = [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
# printing original list
print("The original list is : " + str(test_list))
# end result converted to list object
# filter extracts req. tuples
res = list(filter(lambda sub : len(set(sub)) == 1, test_list))
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
Filtered Tuples : [(6, 6, 6), (1, 1)]
Method #3 : Using count() and len() methods
Approach
- Initiate a for loop to traverse list of tuples
- For each tuple check whether count of first element is equal to length of tuple
- If yes then all elements of tuple are same, append such tuple to output list
- Display output list
Python3
# Python3 code to demonstrate working of
# Filter similar elements Tuples
# initializing list
test_list = [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
# printing original list
print("The original list is : " + str(test_list))
res=[]
for i in test_list:
if(i.count(i[0])==len(i)):
res.append(i)
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
Filtered Tuples : [(6, 6, 6), (1, 1)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 4 : Using a for loop and a set.
Explanation:
We use a for loop to iterate over the tuples in test_list.
Inside the for loop, we create a set from the current tuple using the set() function. A set is an unordered collection of unique elements, so if all elements in the tuple are the same, the set will contain only one element.
We check the length of the set using the len() function. If the length is 1, it means that all elements in the tuple are the same, so we append the tuple to the new list res.
Finally, we print the filtered tuples.
Python3
# Python3 code to demonstrate working of
# Filter similar elements Tuples
# initializing list
test_list = [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
# printing original list
print("The original list is : " + str(test_list))
# Using for loop and set
res = []
for tup in test_list:
if len(set(tup)) == 1:
res.append(tup)
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [(5, 6, 5, 5), (6, 6, 6), (1, 1), (9, 10)]
Filtered Tuples : [(6, 6, 6), (1, 1)]
This approach has a time complexity of O(n*k), where n is the number of tuples in the list and k is the maximum length of a tuple.
The auxiliary space is O(k), because we create a set for each tuple.
Similar Reads
Python - Filter Tuples with All Even Elements Given List of tuples, filter only those with all even elements. Input : test_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 1, 2), (7, )] Output : [(6, 4, 2, 8)] Explanation : Only 1 tuple with all even elements. Input : test_list = [(6, 4, 2, 9), (5, 6, 7, 6), (8, 1, 2), (7, )] Output : [] Explanation : N
9 min read
Python - Filter Tuple with Elements capped on K Given a List of Tuples, extract tuples in which each element is max K. Input : test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)], K = 7 Output : [(4, 5, 3), (3, 4, 7), (4, 3, 2)] Explanation : All tuples have maximum value 7. Input : test_list = [(4, 5, 3), (4, 3, 2), (4, 7, 8)], K = 7 Output
5 min read
Python - Elements with same index Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9]Â Output : [1, 4, 6]Â Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9]Â Output : []Â Explanation : No number at its index. Method
5 min read
Python - Filter rows with required elements Given a Matrix, filter rows with required elements from other list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [4, 6, 2, 8] Output : [[2, 4, 6], [2, 4, 8]] Explanation : All elements are from the check list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4,
6 min read
Python - Extract tuples with elements in Range Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read