Python – Prefix tuple records
Last Updated :
07 May, 2023
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.
Similar Reads
Python | True Record
Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values. This kind of problem is common in data preprocessing steps. Letâs discuss certain ways in which this task can be performed. Method #1 : Using not + any() + map(
6 min read
Python | Intersection in Tuple Records Data
Sometimes, while working with data, we may have a problem in which we require to find the matching records between two lists that we receive. This is a very common problem and records usually occur as a tuple. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list com
7 min read
Python - Remove nested records from tuple
Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Letâs discuss certain way in which this task c
5 min read
Python | Record Similar tuple occurrences
Sometimes, while working with data, we can have a problem in which we need to check the occurrences of records that occur at similar times. This has applications in the web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + Counter() + sorte
9 min read
Python - Count elements in record tuple
Sometimes, while working with data in form of records, we can have a problem in which we need to find the total element counts of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method
5 min read
Python | Records Union
Sometimes, while working with data, we may have a problem in which we require to find all records between two lists that we receive. This is a very common problem and records usually occur as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1: Using list comprehension
6 min read
Python | Records Intersection
Sometimes, while working with tuples, we can have a problem in which we need similar features of two records. This type of application can come in the Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1: Using set() + "&" operator This task can be perfo
6 min read
Python | Compare tuples
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read
Python - Modify Equal Tuple Rows
Sometimes, while working with Python Records, we can have a problem in which we need to perform the modification of element records on equality of records. This can have a problem in domains that involve data. Let's discuss certain ways in which this task can be performed. Input : test_list = [[(12,
3 min read
Python | Replace duplicates in tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to remove tuples elements that occur more than one times and replace duplicas with some custom value. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + list comprehension The c
5 min read