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
# Python3 code to demonstrate working of
# Prefix tuple records
# Using list comprehension + zip() + all()
# initializing list
test_list = [('Gfg', 'best', 'geeks'), ('Gfg', 'good'),
('Gfg', 'best', 'CS'), ('Gfg', 'love')]
# printing original list
print("The original list is : " + str(test_list))
# initializing prefix tuple
pref_tup = ('Gfg', 'best')
# Prefix tuple records
# Using list comprehension + zip() + all()
res = [tup for tup in test_list if all(x == y for x, y in
zip(tup, pref_tup))]
# printing result
print("The filtered tuples : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Prefix tuple records
# Using filter() + lambda + generator expression + all()
# initializing list
test_list = [('Gfg', 'best', 'geeks'), ('Gfg', 'good'),
('Gfg', 'best', 'CS'), ('Gfg', 'love')]
# printing original list
print("The original list is : " + str(test_list))
# initializing prefix tuple
pref_tup = ('Gfg', 'best')
# Prefix tuple records
# Using filter() + lambda + generator expression + all()
res = list(filter(lambda sub: all([sub[idx] == ele
for idx, ele in enumerate(pref_tup)]), test_list))
# printing result
print("The filtered tuples : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Prefix tuple records
# initializing list
test_list = [('Gfg', 'best', 'geeks'), ('Gfg', 'good'),
('Gfg', 'best', 'CS'), ('Gfg', 'love')]
# printing original list
print("The original list is : " + str(test_list))
# initializing prefix tuple
pref_tup = ('Gfg', 'best')
# Prefix tuple records
pref_tup=" ".join(pref_tup)
x=[]
res=[]
for i in test_list:
a=" ".join(i)
if(a.startswith(pref_tup)):
res.append(i)
# printing result
print("The filtered tuples : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Prefix tuple records
# initializing list
test_list = [('Gfg', 'best', 'geeks'), ('Gfg', 'good'),
('Gfg', 'best', 'CS'), ('Gfg', 'love')]
# printing original list
print("The original list is : " + str(test_list))
# initializing prefix tuple
pref_tup = ('Gfg', 'best')
# Prefix tuple records
pref_tup = " ".join(pref_tup)
x = []
res = []
for i in test_list:
a = " ".join(i)
if(a.find(pref_tup) == 0):
res.append(i)
# printing result
print("The filtered tuples : " + str(res))
OutputThe 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
# initializing list
test_list = [('Gfg', 'best', 'geeks'), ('Gfg', 'good'),
('Gfg', 'best', 'CS'), ('Gfg', 'love')]
# printing original list
print("The original list is : " + str(test_list))
# initializing prefix tuple
pref_tup = ('Gfg', 'best')
# Prefix tuple records using a for loop
res = []
for i in test_list:
if i[0:2] == pref_tup:
res.append(i)
# printing result
print("The filtered tuples : " + str(res))
OutputThe 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 - 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 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 | 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