Open In App

Python – Prefix tuple records

Improve
Improve
Like Article
Like
Save
Share
Report

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))


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




# 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))


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




# 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))


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




# 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))


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.

  1. Create a list of tuples containing string elements and assign it to variable test_list.
  2. Print the original list using the print() function and the string “The original list is : ” concatenated with the str(test_list) expression.
  3. Create a tuple containing two string elements and assign it to variable pref_tup.
  4. 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.
  5. If the elements match, append the tuple to a new list called res.
  6. 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))


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
Previous
Next
Share your thoughts in the comments
Similar Reads