Given List of tuples, filter tuples, whose element strings have length equal to K.
Input : test_list = [(“ABC”, “Gfg”, “CS1”), (“Gfg”, “Best”), (“Gfg”, “WoOW”)], K = 3
Output : [(‘ABC’, ‘Gfg’, ‘CS1’)]
Explanation : All Strings have length 3 in above tuple.
Input : test_list = [(“ABCD”, “Gfg”, “CS1”), (“Gfg”, “Best”), (“Gfg”, “WoOW”)], K = 3
Output : []
Explanation : No Strings have length 3 in above tuples.
Method #1: Using loop
In this, we run nested loop to test each string, if equals to K, then the tuple is appended to result list, else its omitted.
Python3
test_list = [( "ABC" , "Gfg" , "CS1" ), ( "Gfg" , "Best" ), ( "Gfg" , "WoW" )]
print ( "The original list is : " + str (test_list))
K = 3
res_list = []
for sub in test_list:
res = True
for ele in sub:
if len (ele) ! = K :
res = False
break
if res:
res_list.append(sub)
print ( "The filtered tuples : " + str (res_list))
|
OutputThe original list is : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'Best'), ('Gfg', 'WoW')]
The filtered tuples : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'WoW')]
Time Complexity: O(n*n), where n is the elements of tuples
Auxiliary Space: O(n), where n is the size of tuple
Method #2 : Using all() + list comprehension
Compacted way, uses all() to check for lengths equivalence and iteration using list comprehension.
Python3
test_list = [( "ABC" , "Gfg" , "CS1" ), ( "Gfg" , "Best" ), ( "Gfg" , "WoW" )]
print ( "The original list is : " + str (test_list))
K = 3
res = [sub for sub in test_list if all ( len (ele) = = K for ele in sub)]
print ( "The filtered tuples : " + str (res))
|
OutputThe original list is : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'Best'), ('Gfg', 'WoW')]
The filtered tuples : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'WoW')]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method 3: using the filter() function and lambda expression.
Define the list of tuples test_list and print it using the print() function.
Define the value of K.
Use the filter() function to filter the tuples in test_list based on a condition.
The condition to be checked is given by the lambda expression: lambda x: all(len(ele) == K for ele in x). This expression checks if all elements (i.e., strings) in each tuple have a length equal to K. If the condition is True for a tuple, it is included in the result.
Convert the result to a list using the list() function and store it in the variable res.
Print the filtered tuples using the print() function.
Python3
test_list = [( "ABC" , "Gfg" , "CS1" ), ( "Gfg" , "Best" ), ( "Gfg" , "WoW" )]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( filter ( lambda x: all ( len (ele) = = K for ele in x), test_list))
print ( "The filtered tuples : " + str (res))
|
OutputThe original list is : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'Best'), ('Gfg', 'WoW')]
The filtered tuples : [('ABC', 'Gfg', 'CS1'), ('Gfg', 'WoW')]
The time complexity of this approach is O(nk), where n is the number of tuples and k is the maximum length of the elements in the tuple.
The auxiliary space used by this approach is O(n), where n is the number of tuples.
METHOD 4:Using quicksort method
APPROACH:
This code takes a list of tuples as input and filters the tuples whose concatenated string length is equal to k times the length of the tuple. The filtering is done using the quicksort algorithm and list comprehension in Python.
ALGORITHM:
1. Define the input list of tuples and the value of k.
2. Define the quicksort() function that takes a list of tuples and sorts them based on the length of their concatenated string.
3. The quicksort() function uses the pivot element to partition the list into three sublists: those with shorter concatenated strings, those with equal concatenated string length, and those with longer concatenated strings.
4. The function then recursively sorts the left and right sublists and combines the sorted sublists with the middle list to obtain the sorted list.
5. Define the filtered_lst list that filters the sorted list and selects the tuples whose concatenated string length is equal to k times the length of the tuple using list comprehension.
6. Output the filtered tuples.
Python3
lst = [( 'ABC' , 'Gfg' , 'CS1' ), ( 'Gfg' , 'Best' ), ( 'Gfg' , 'WoW' )]
k = 3
def quicksort(arr):
if len (arr) < = 1 :
return arr
pivot = arr[ len (arr) / / 2 ]
left = [tpl for tpl in arr if len (' '.join(tpl)) < len(' '.join(pivot))]
middle = [tpl for tpl in arr if len (' '.join(tpl)) == len(' '.join(pivot))]
right = [tpl for tpl in arr if len (' '.join(tpl)) > len(' '.join(pivot))]
return quicksort(left) + middle + quicksort(right)
filtered_lst = [tpl for tpl in quicksort(lst) if len (''.join(tpl)) = = k * len (tpl)]
print ( "Filtered tuples:" , filtered_lst)
|
OutputFiltered tuples: [('Gfg', 'WoW'), ('ABC', 'Gfg', 'CS1')]
Time complexity:
The time complexity of quicksort algorithm is O(n*logn) in the average case and O(n^2) in the worst case. In this case, the worst case is unlikely to occur as we are partitioning the list based on the length of the concatenated string, which is not dependent on the order of the elements in the list.
Space complexity:
The space complexity of the algorithm is O(n) because we are creating new lists for the left, middle, and right sublists in each recursive call to quicksort(). In addition, the filtered_lst list also takes up space