Python - Prefix key match in dictionary

Prefix key matching in Python dictionaries allows you to find all keys that start with a specific string pattern. This technique is useful for filtering data, implementing autocomplete features, or searching through structured datasets. Let's explore three effective approaches to implement prefix key matching.

Using Linear Search with startswith()

The simplest approach iterates through all dictionary keys and checks if each key begins with the specified prefix using Python's built-in startswith() method.

Example

def prefix_match_linear(dictionary, prefix):
    matches = []
    for key in dictionary.keys():
        if key.startswith(prefix):
            matches.append(key)
    return matches

fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

prefix = 'app'
matches = prefix_match_linear(fruit_dict, prefix)
print("Keys starting with 'app':", matches)
Keys starting with 'app': ['apple']

Using Trie Data Structure

A Trie (prefix tree) is a tree-like data structure optimized for prefix matching operations. It provides efficient searching, especially when dealing with large datasets or multiple prefix queries.

Example

class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end_of_word = False

class Trie:
    def __init__(self):
        self.root = TrieNode()
    
    def insert(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TrieNode()
            node = node.children[char]
        node.is_end_of_word = True
    
    def get_matches(self, prefix):
        node = self.root
        matches = []
        
        # Navigate to the prefix end
        for char in prefix:
            if char not in node.children:
                return matches
            node = node.children[char]
        
        # Collect all words with this prefix
        self._collect_matches(node, prefix, matches)
        return matches
    
    def _collect_matches(self, node, prefix, matches):
        if node.is_end_of_word:
            matches.append(prefix)
        for char, child in node.children.items():
            self._collect_matches(child, prefix + char, matches)

def prefix_match_trie(dictionary, prefix):
    trie = Trie()
    for key in dictionary.keys():
        trie.insert(key)
    return trie.get_matches(prefix)

fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

prefix = 'ap'
matches = prefix_match_trie(fruit_dict, prefix)
print("Keys starting with 'ap':", matches)
Keys starting with 'ap': ['apple', 'apricot']

Using filter() Function

Python's built-in filter() function provides a concise way to perform prefix matching using a lambda function for the filtering condition.

Example

def prefix_match_filter(dictionary, prefix):
    return list(filter(lambda key: key.startswith(prefix), dictionary.keys()))

fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

prefix = 'p'
matches = prefix_match_filter(fruit_dict, prefix)
print("Keys starting with 'p':", matches)
Keys starting with 'p': ['pineapple']

Performance Comparison

Method Time Complexity Space Complexity Best For
Linear Search O(n × m) O(1) Small datasets, simple implementation
Trie O(p + k × m) O(total characters) Multiple queries, large datasets
Filter O(n × m) O(1) Functional programming style

Where n = number of keys, m = average key length, p = prefix length, k = number of matches

Conclusion

Choose the linear search or filter approach for simple, one-time prefix matching operations. Use the Trie data structure when you need to perform multiple prefix queries on the same dataset, as it offers superior performance for repeated searches.

Updated on: 2026-03-27T14:23:53+05:30

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements