Group Similar Start and End Character Words using Python

In Python, we can group words with similar start and end characters using methods like dictionaries and loops, regular expressions, and list comprehensions. The task involves analyzing a collection of words and identifying groups of words that share common starting and ending characters. This can be useful in text classification, information retrieval, and spell-checking applications.

Using Dictionaries and Loops

This method utilizes a dictionary to group words based on their similar start and end characters. By iterating through the list of words and extracting the start and end characters of each word, we create a tuple key for the dictionary.

Example

In the below example, we define a function group_words that takes a list of words as input. We initialize an empty dictionary to store the groups. For each word, we extract the start character (word[0]) and end character (word[-1]) to create a tuple key ?

def group_words(words):
    groups = {}
    for word in words:
        start_char = word[0]
        end_char = word[-1]
        key = (start_char, end_char)
        if key in groups:
            groups[key].append(word)
        else:
            groups[key] = [word]
    return groups

words = ['apple', 'banana', 'ant', 'cat', 'dog', 'elephant', 'amazon', 'grape']
result = group_words(words)
print(result)

The output of the above code is ?

{('a', 'e'): ['apple'], ('b', 'a'): ['banana'], ('a', 't'): ['ant'], ('c', 't'): ['cat'], ('d', 'g'): ['dog'], ('e', 't'): ['elephant'], ('a', 'n'): ['amazon'], ('g', 'e'): ['grape']}

Using Regular Expressions

Regular expressions can be used to match patterns within each word. We define a specific pattern to capture the start and end characters of a word and extract those characters for grouping.

Example

We utilize the re module to match the start and end characters using the pattern ^(.)(.*)(.)$. This pattern captures the first character, any middle characters, and the last character ?

import re

def group_words(words):
    groups = {}
    for word in words:
        match = re.match(r'^(.)(.*)(.)$', word)
        if match:
            start_char = match.group(1)
            end_char = match.group(3)
            key = (start_char, end_char)
            if key in groups:
                groups[key].append(word)
            else:
                groups[key] = [word]
    return groups

words = ['apple', 'banana', 'ant', 'cat', 'dog', 'elephant', 'amazon', 'grape']
result = group_words(words)
print(result)

The output of the above code is ?

{('a', 'e'): ['apple'], ('b', 'a'): ['banana'], ('a', 't'): ['ant'], ('c', 't'): ['cat'], ('d', 'g'): ['dog'], ('e', 't'): ['elephant'], ('a', 'n'): ['amazon'], ('g', 'e'): ['grape']}

Using List Comprehensions

List comprehensions offer a concise way to group words. We can create a dictionary with all unique key combinations first, then populate it with the corresponding words.

Example

We first create a dictionary with all possible keys using dictionary comprehension, then use list comprehension to populate each group ?

def group_words(words):
    groups = {(word[0], word[-1]): [] for word in words}
    [groups[(word[0], word[-1])].append(word) for word in words]
    return groups

words = ['apple', 'banana', 'ant', 'cat', 'dog', 'elephant', 'amazon', 'grape']
result = group_words(words)
print(result)

The output of the above code is ?

{('a', 'e'): ['apple'], ('b', 'a'): ['banana'], ('a', 't'): ['ant'], ('c', 't'): ['cat'], ('d', 'g'): ['dog'], ('e', 't'): ['elephant'], ('a', 'n'): ['amazon'], ('g', 'e'): ['grape']}

Comparison

Method Readability Performance Best For
Dictionaries & Loops High Good Clear, readable code
Regular Expressions Medium Slower Complex pattern matching
List Comprehensions Medium Good Concise, Pythonic style

Conclusion

We explored three methods to group words by their start and end characters: dictionaries with loops, regular expressions, and list comprehensions. The dictionary approach offers the best balance of readability and performance for most use cases.

Updated on: 2026-03-27T08:14:56+05:30

370 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements