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 | 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 | 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 - 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 | 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 | Removing strings from tuple
Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let's discuss certain ways in which this particular task can b
4 min read
Python - Convert String Records to Tuples Lists
Sometimes, while working with data, we can have problem in which we need to convert the data list which in string format to list of tuples. This can occur in domains in which we have cross type inputs. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + eval() The
7 min read
Creating Sets of Tuples in Python
Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
Python - Records with Value at K index
Sometimes, while working with records, we might have a problem in which we need to find all the tuples of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Letâs discuss c
8 min read
Python - N Random Tuples list
Sometimes, while working with Python records, we can have a problem in which we need to construct a random tuple list. This can have applications in many domains using gaming and day-to-day programming. Let's discuss specific ways in which this task can be performed in Python. Input: N = 4 R = 6 Out
3 min read