Python - Prefix Tuple Records

In this article, we will explore various methods to find tuple records that start with a specific prefix using Python. This is useful when working with datasets where you need to filter records based on string patterns.

A tuple is an immutable sequence whose values cannot be changed once assigned. A prefix tuple record refers to tuples where the first element (typically a string) starts with a common prefix.

Problem Statement

Given a list of tuples and a prefix string, we need to find all tuples whose first element starts with that prefix ?

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

# Expected result: [('Komal', 456), ('Kosal', 321)]

Method 1: Using startswith()

The most straightforward approach uses the startswith() method to check if each tuple's first element begins with the given prefix ?

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

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

Method 2: Using List Comprehension

A more concise approach using list comprehension for the same logic ?

def find_prefix_tuples(tuples, prefix):
    return [record for record in tuples if record[0].startswith(prefix)]

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

Method 3: Using Filter and Lambda

The filter() function with a lambda expression provides a functional programming approach ?

def find_prefix_tuples(tuples, prefix):
    return list(filter(lambda t: t[0].startswith(prefix), tuples))

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

Method 4: Using String Slicing

Compare the first few characters using string slicing ?

def find_prefix_tuples(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(find_prefix_tuples(records, prefix))
[('Komal', 456), ('Kosal', 321)]

Method 5: Using Regular Expressions

Regular expressions provide powerful pattern matching for complex prefix requirements ?

import re

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

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

Performance Comparison

Method Readability Performance Best For
startswith() High Fast Simple prefix matching
List comprehension High Fast Concise filtering
filter() Medium Fast Functional programming style
String slicing Medium Fast Learning string operations
Regular expressions Low Slower Complex pattern matching

Conclusion

For simple prefix matching, use startswith() with list comprehension for the best balance of readability and performance. Use regular expressions only when you need complex pattern matching beyond simple prefixes.

Updated on: 2026-03-27T15:12:14+05:30

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements