How to Group Strings on Kth character using Python?

In Python, grouping strings based on their Kth character is a common string manipulation task. We can accomplish this using several approaches: regular dictionaries, defaultdict from collections, and itertools.groupby(). Each method offers different advantages depending on your specific needs.

Method 1: Using a Dictionary

The simplest approach uses a regular dictionary to group strings by their Kth character. We manually check if a key exists before adding strings to the corresponding group.

Example

Here's how to group strings by their 2nd character using a dictionary ?

def group_strings_on_kth_char(strings, k):
    grouped_strings = {}
    for string in strings:
        key = string[k-1]  # Adjusting for zero-based indexing
        if key not in grouped_strings:
            grouped_strings[key] = []
        grouped_strings[key].append(string)
    return grouped_strings

strings = ['apple', 'banana', 'avocado', 'cherry', 'orange', 'mango']
k = 2
result = group_strings_on_kth_char(strings, k)
print(result)
{'p': ['apple'], 'a': ['banana', 'mango'], 'v': ['avocado'], 'h': ['cherry'], 'r': ['orange']}

Method 2: Using defaultdict

The defaultdict automatically creates empty lists for new keys, eliminating the need for explicit key checking. This makes the code cleaner and more efficient.

Example

Using defaultdict simplifies the grouping logic ?

from collections import defaultdict

def group_strings_on_kth_char(strings, k):
    grouped_strings = defaultdict(list)
    for string in strings:
        key = string[k-1]  # Adjusting for zero-based indexing
        grouped_strings[key].append(string)
    return grouped_strings

strings = ['apple', 'banana', 'avocado', 'cherry', 'orange', 'mango']
k = 2
result = group_strings_on_kth_char(strings, k)
print(dict(result))  # Convert to regular dict for cleaner output
{'p': ['apple'], 'a': ['banana', 'mango'], 'v': ['avocado'], 'h': ['cherry'], 'r': ['orange']}

Method 3: Using itertools.groupby()

The itertools.groupby() function groups consecutive elements with the same key. Since it requires sorted input, we must first sort the strings by their Kth character.

Example

This approach sorts first, then groups consecutive elements ?

import itertools

def group_strings_on_kth_char(strings, k):
    strings.sort(key=lambda x: x[k-1])  # Sort by Kth character
    grouped_strings = {}
    for key, group in itertools.groupby(strings, key=lambda x: x[k-1]):
        grouped_strings[key] = list(group)
    return grouped_strings

strings = ['apple', 'banana', 'avocado', 'cherry', 'orange', 'mango']
k = 2
result = group_strings_on_kth_char(strings, k)
print(result)
{'a': ['banana', 'mango'], 'h': ['cherry'], 'p': ['apple'], 'r': ['orange'], 'v': ['avocado']}

Comparison

Method Code Complexity Performance Best For
Dictionary Medium Good Simple use cases
defaultdict Low Best Most scenarios
itertools.groupby Medium Good When sorted output needed

Conclusion

Use defaultdict for the cleanest and most efficient solution. Choose itertools.groupby() when you need sorted groups, and regular dictionaries for simple cases where you want explicit control over key creation.

Updated on: 2026-03-27T08:36:00+05:30

473 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements