Python – Prefix tuple records
Sometimes, while working with Python lists, we can have problem in which we need to find all the tuples which begin with a particular tuple record. This kind of problem can find application in data domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + zip() + all()
The combination of above functionalities can be used to solve this problem. In this, we perform the task of checking using all(), which checks for all prefix elements equality with elements zipped with prefixes using zip(). The list comprehension is used to perform for all the elements.
Python3
test_list = [( 'Gfg' , 'best' , 'geeks' ), ( 'Gfg' , 'good' ),
( 'Gfg' , 'best' , 'CS' ), ( 'Gfg' , 'love' )]
print ( "The original list is : " + str (test_list))
pref_tup = ( 'Gfg' , 'best' )
res = [tup for tup in test_list if all (x = = y for x, y in
zip (tup, pref_tup))]
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('Gfg', 'best', 'geeks'), ('Gfg', 'good'), ('Gfg', 'best', 'CS'), ('Gfg', 'love')]
The filtered tuples : [('Gfg', 'best', 'geeks'), ('Gfg', 'best', 'CS')]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using filter() + lambda + generator expression + all()
The combination of above functions can be used to solve this particular problem. In this, we perform the task of filtering using filter() and logic compilation using lambda function.
Python3
test_list = [( 'Gfg' , 'best' , 'geeks' ), ( 'Gfg' , 'good' ),
( 'Gfg' , 'best' , 'CS' ), ( 'Gfg' , 'love' )]
print ( "The original list is : " + str (test_list))
pref_tup = ( 'Gfg' , 'best' )
res = list ( filter ( lambda sub: all ([sub[idx] = = ele
for idx, ele in enumerate (pref_tup)]), test_list))
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('Gfg', 'best', 'geeks'), ('Gfg', 'good'), ('Gfg', 'best', 'CS'), ('Gfg', 'love')]
The filtered tuples : [('Gfg', 'best', 'geeks'), ('Gfg', 'best', 'CS')]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The list comprehension is used to perform the task and it takes O(n) time.
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 startswith() method
Python3
test_list = [( 'Gfg' , 'best' , 'geeks' ), ( 'Gfg' , 'good' ),
( 'Gfg' , 'best' , 'CS' ), ( 'Gfg' , 'love' )]
print ( "The original list is : " + str (test_list))
pref_tup = ( 'Gfg' , 'best' )
pref_tup = " " .join(pref_tup)
x = []
res = []
for i in test_list:
a = " " .join(i)
if (a.startswith(pref_tup)):
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('Gfg', 'best', 'geeks'), ('Gfg', 'good'), ('Gfg', 'best', 'CS'), ('Gfg', 'love')]
The filtered tuples : [('Gfg', 'best', 'geeks'), ('Gfg', 'best', 'CS')]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The startswith() method is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #4 : Using find() method
Python3
test_list = [( 'Gfg' , 'best' , 'geeks' ), ( 'Gfg' , 'good' ),
( 'Gfg' , 'best' , 'CS' ), ( 'Gfg' , 'love' )]
print ( "The original list is : " + str (test_list))
pref_tup = ( 'Gfg' , 'best' )
pref_tup = " " .join(pref_tup)
x = []
res = []
for i in test_list:
a = " " .join(i)
if (a.find(pref_tup) = = 0 ):
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('Gfg', 'best', 'geeks'), ('Gfg', 'good'), ('Gfg', 'best', 'CS'), ('Gfg', 'love')]
The filtered tuples : [('Gfg', 'best', 'geeks'), ('Gfg', 'best', 'CS')]
Time complexity: O(n*m), where n is the length of the list and m is the length of the prefix tuple.
Auxiliary space: O(n), where n is the length of the list.
Method #5: Using a for loop
One additional method to solve this problem is by using a for loop to iterate through each tuple in the list and check if the first elements of the tuple match the first two elements of the prefix tuple. If they match, the tuple is added to the result list.
- Create a list of tuples containing string elements and assign it to variable test_list.
- Print the original list using the print() function and the string “The original list is : ” concatenated with the str(test_list) expression.
- Create a tuple containing two string elements and assign it to variable pref_tup.
- Using a for loop and the append() method, iterate through each tuple in the list and check if the first two elements of the tuple match the elements in the prefix tuple.
- If the elements match, append the tuple to a new list called res.
- Print the filtered tuples using the print() function and the string “The filtered tuples : ” concatenated with the str(res) expression.
Python3
test_list = [( 'Gfg' , 'best' , 'geeks' ), ( 'Gfg' , 'good' ),
( 'Gfg' , 'best' , 'CS' ), ( 'Gfg' , 'love' )]
print ( "The original list is : " + str (test_list))
pref_tup = ( 'Gfg' , 'best' )
res = []
for i in test_list:
if i[ 0 : 2 ] = = pref_tup:
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('Gfg', 'best', 'geeks'), ('Gfg', 'good'), ('Gfg', 'best', 'CS'), ('Gfg', 'love')]
The filtered tuples : [('Gfg', 'best', 'geeks'), ('Gfg', 'best', 'CS')]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(k), where k is the number of tuples that match the prefix tuple.
Last Updated :
07 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...