Python - Prefix Tuple Records


In this article we will get to know about various method using which we can find the prefix tuple records using python programming language.

Tuple is an immutable sequence-like list whose value cannot be changed once assigned. Here prefix tuple is a collection of tuples which have a common prefix.

Each element of tuple can be of any data type or it can also be in mixed form.

Example

records = [('Kalyan', 123),('Gungun', 122), ('Komal', 456), ('Isestru', 789), ('Kosal', 321)]
prefix = 'Ko'

Here you can see we have list of records where tuples are there in pairs and we have prefix as ‘Ko’. we have to find all the tuples whose prefix start with the letter ‘Ko’. Here our result will be

Output

[('Komal', 456), ('Kosal', 321)]

Let’s see some of the methods using which we can achieve this.

Method 1: Using List Comprehension

Using list comprehension we can create prefix tuple record. We will iterate over the given list of tuples and check if the tuples start with given prefix, if yes then collect the machine tuples.

Example

def pref_tuple(tuples, prefix):
   result = []
   for record in tuples:
      string=record[0]
      if string.startswith(prefix):
         result.append(record)
   return result

records = [('Kalyan', 123),('Gungun', 122), ('Komal', 456), ('Isestru', 789), ('Kosal', 321)]
prefix = 'Ko'
print(pref_tuple(records, prefix))

Output

[('Komal', 456), ('Kosal', 321)]

Explanation

Here in the program our function takes an input list of tuples and a prefix. We iterate over each value in the tuples list and for each tuple it will take out the first value and check if its prefix is starts with the given prefix value and if it becomes true then it will get added to result list.

Method 2: Using Filter and Lambda

In this method we will use the filter function along with the lambda function to filter out the tuples which start with the given prefix. The filter() function is applied with the lambda function on each element in the list of tuples. It returns the element for which the lambda function became true means the string containing the prefix will be returned as result.

Example

def pref_tuple(tuples, prefix):
   return list(filter(lambda t: t.startswith(prefix), tuples))

records = [('Kalyan', 123),('Gungun', 122), ('Komal', 456), ('Isestru', 789), ('Kosal', 321)]
prefix = 'Ko'
result = pref_tuple(records, prefix)
print(result)

Output

[('Komal', 456), ('Kosal', 321)]

Explanation

Here in the program our function takes an input list of tuples and a prefix. We are using filter() function along with lambda function to filter out the values which satisfy the criteria of starting with the given prefix.

Method 3: List Slicing

In this method we will extract the substring of each tuple and compare it with the given prefix Using this approach we can extract the tuples which contain the prefix as given prefix value.

Example

def pref_tuple(tuples, prefix):
   return [t for t in tuples if t[0:len(prefix)] == prefix]

records = [('Kalyan', 123),('Gungun', 122), ('Komal', 456), ('Isestru', 789), ('Kosal', 321)]
prefix = 'Ko'
print(pref_tuple(records, prefix))  

Output

[]

Explanation

Here in the above program our function takes two parameter list of tuple and a prefix string. We are traversing over the string and using the concept of slicing t[0:len(prefix)] we are comparing with the prefix.

Method 4: Regular Expressions

This is very efficient approach for any pattern matching operations. This allows us to find the pattern or prefix of complex or long strings also.

Example

def pref_tuple(tuples, prefix):
   pattern = f'^{prefix}'
   return [t for t in tuples if re.match(pattern, t)]

records = [('Kalyan', 123),('Gungun', 122), ('Komal', 456), ('Isestru', 789), ('Kosal', 321)]
prefix = 'Ko'
print(pref_tuple(records, prefix))  

Output

[('Komal', 456), ('Kosal', 321)]

Explanation

Here our function is taking two parameter lists of tuples and a prefix list and inside the function we will create a regular expression which will find the pattern starting with the given prefix name. And compare if it is matching if yes then we will include that into the list and return.

So, we saw various methods using which we can find the tuples which contain the prefix as given prefix value.

Updated on: 13-Oct-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements