Python - Group first elements by second elements in Tuple list

In Python, grouping first elements by second elements in a tuple list means organizing tuples that share the same second element into groups. For example, if we have tuples like ('Apple', 'Fruit') and ('Banana', 'Fruit'), we want to group ['Apple', 'Banana'] under the key 'Fruit'.

Using a Dictionary

This approach uses a dictionary where the second element becomes the key, and first elements are stored as values in a list ?

# Sample tuple list
data = [('Apple', 'Fruit'), ('Banana', 'Fruit'), ('Carrot', 'Vegetable'), ('Potato', 'Vegetable')]

# Grouping elements using a dictionary
grouped_data = {}
for item in data:
    key = item[1]
    value = item[0]
    if key in grouped_data:
        grouped_data[key].append(value)
    else:
        grouped_data[key] = [value]

# Display the grouped data
for key, values in grouped_data.items():
    print(f"{key}: {values}")
Fruit: ['Apple', 'Banana']
Vegetable: ['Carrot', 'Potato']

Using itertools.groupby()

The groupby() function groups consecutive elements with the same key. The data must be sorted first ?

from itertools import groupby

# Sample tuple list
data = [('Apple', 'Fruit'), ('Banana', 'Fruit'), ('Carrot', 'Vegetable'), ('Potato', 'Vegetable')]

# Sort the data based on the second element
sorted_data = sorted(data, key=lambda x: x[1])

# Group elements using itertools.groupby()
grouped_data = {}
for key, group in groupby(sorted_data, key=lambda x: x[1]):
    grouped_data[key] = [item[0] for item in group]

# Display the grouped data
for key, values in grouped_data.items():
    print(f"{key}: {values}")
Fruit: ['Apple', 'Banana']
Vegetable: ['Carrot', 'Potato']

Using defaultdict from collections

The defaultdict automatically creates an empty list for new keys, eliminating the need for manual key checking ?

from collections import defaultdict

# Sample tuple list
data = [('Apple', 'Fruit'), ('Banana', 'Fruit'), ('Carrot', 'Vegetable'), ('Potato', 'Vegetable')]

# Group elements using defaultdict
grouped_data = defaultdict(list)
for item in data:
    grouped_data[item[1]].append(item[0])

# Display the grouped data
for key, values in grouped_data.items():
    print(f"{key}: {values}")
Fruit: ['Apple', 'Banana']
Vegetable: ['Carrot', 'Potato']

Comparison

Method Requires Sorting? Code Simplicity Best For
Dictionary No Medium General purpose
groupby() Yes Medium Already sorted data
defaultdict No High Clean, readable code

Conclusion

Use defaultdict for the cleanest code, regular dictionaries for explicit control, and groupby() when working with sorted data. All three methods effectively group first elements by second elements in tuple lists.

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

928 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements