Python - Group Tuples by Kth Index Element

In Python, we can group tuples by their kth index element using several methods like dictionaries, itertools.groupby(), and defaultdict. This technique is useful for data analysis and manipulation when organizing data based on specific tuple elements.

Using a Dictionary

The simplest approach uses a dictionary where we iterate through tuples and use the kth index element as the key ?

def group_tuples_by_kth_index(tuples, k):
    groups = {}
    for t in tuples:
        key = t[k]
        if key not in groups:
            groups[key] = []
        groups[key].append(t)
    return groups

# Example usage
tuples_list = [('apple', 10), ('banana', 20), ('apple', 15), ('banana', 25), ('cherry', 30)]
k = 0

result = group_tuples_by_kth_index(tuples_list, k)
print(result)
{'apple': [('apple', 10), ('apple', 15)], 'banana': [('banana', 20), ('banana', 25)], 'cherry': [('cherry', 30)]}

Using itertools.groupby()

The itertools.groupby() function groups consecutive elements. We must sort tuples first by the kth index element ?

from itertools import groupby

def group_tuples_by_kth_index(tuples, k):
    sorted_tuples = sorted(tuples, key=lambda x: x[k])
    groups = {key: list(group) for key, group in groupby(sorted_tuples, key=lambda x: x[k])}
    return groups

# Example usage
tuples_list = [('apple', 10), ('banana', 20), ('apple', 15), ('banana', 25), ('cherry', 30)]
k = 0

result = group_tuples_by_kth_index(tuples_list, k)
print(result)
{'apple': [('apple', 10), ('apple', 15)], 'banana': [('banana', 20), ('banana', 25)], 'cherry': [('cherry', 30)]}

Using defaultdict

The defaultdict automatically creates empty lists for new keys, eliminating the need for explicit checks ?

from collections import defaultdict

def group_tuples_by_kth_index(tuples, k):
    groups = defaultdict(list)
    for t in tuples:
        groups[t[k]].append(t)
    return dict(groups)  # Convert to regular dict for cleaner output

# Example usage
tuples_list = [('apple', 10), ('banana', 20), ('apple', 15), ('banana', 25), ('cherry', 30)]
k = 0

result = group_tuples_by_kth_index(tuples_list, k)
print(result)
{'apple': [('apple', 10), ('apple', 15)], 'banana': [('banana', 20), ('banana', 25)], 'cherry': [('cherry', 30)]}

Grouping by Different Index

We can group by any index position. Here's an example grouping by the second element (k=1) ?

from collections import defaultdict

# Group by quantity (index 1)
tuples_list = [('apple', 10), ('banana', 20), ('apple', 15), ('banana', 10), ('cherry', 20)]
k = 1

groups = defaultdict(list)
for t in tuples_list:
    groups[t[k]].append(t)

result = dict(groups)
print(result)
{10: [('apple', 10), ('banana', 10)], 20: [('banana', 20), ('cherry', 20)], 15: [('apple', 15)]}

Comparison

Method Requires Sorting Memory Efficient Best For
Dictionary No Yes Simple grouping
itertools.groupby() Yes Yes Large datasets
defaultdict No Yes Cleaner code

Conclusion

Use defaultdict for the cleanest code, regular dictionaries for simplicity, or itertools.groupby() for sorted data. All methods effectively group tuples by their kth index element with similar performance characteristics.

Updated on: 2026-03-27T08:51:38+05:30

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements