Python - Group Similar Items to Dictionary Values List


In data analysis and processing, it is necessary to group similar elements together for better organization and analysis of data. Python provides several methods to group elements into a dictionary value list efficiently in Python, using for loop, using defaultdict, and using itertools.groupby methods. In this article, we will explore different approaches to Group Similar items to Dictionary Values List in Python.

Method 1: Using a for loop

The simplest way to group similar items into dictionary value lists is by using a for loop. Let's consider an example where we have a list of fruits, and we want to group them based on their respective colors.

Syntax

list_name.append(element)

Here, the append() function is a list method used to add an element to the end of the list_name. It modifies the original list by adding the specified element as a new item.

Example

In the below example, we iterate through each fruit in the "fruits" list. For each fruit, we extract its color. If the color already exists in the "color_dict" dictionary, we append the fruit's name to the corresponding list. Otherwise, we create a new key−value pair in the dictionary, where the key is the color and the value is a list containing the fruit's name.

fruits = [
    {"name": "apple", "color": "red"},
    {"name": "banana", "color": "yellow"},
    {"name": "grape", "color": "purple"},
    {"name": "orange", "color": "orange"},
    {"name": "kiwi", "color": "green"},
    {"name": "strawberry", "color": "red"}
]

color_dict = {}

for fruit in fruits:
    color = fruit["color"]
    if color in color_dict:
        color_dict[color].append(fruit["name"])
    else:
        color_dict[color] = [fruit["name"]]

print(color_dict)

Output

{'red': ['apple', 'strawberry'], 'yellow': ['banana'], 'purple': ['grape'], 'orange': ['orange'], 'green': ['kiwi']}

Method 2: Using defaultdict from collections module

Using Python's collections module we can use defaultdict class that simplifies the process of grouping items. This class automatically initializes a dictionary with a default value for any new key.

Syntax

groups = defaultdict(list)
groups[item].append(item)

Here, the defaultdict() function creates a defaultobject called groups. The second line of code uses the key (item) to access the list associated with that key in the groups dictionary and appends the item to the list.

Example

In the below example, by using the defaultdict class, we eliminate the need for an explicit check before adding items to the dictionary. We initialize the "color_dict" as a defaultdict with a list as its default value. When a new key is accessed, if it doesn't exist, the defaultdict automatically creates an empty list as its value. Thus, we can directly append the fruit's name to the corresponding list without worrying about key existence.

from collections import defaultdict

fruits = [
    {"name": "apple", "color": "red"},
    {"name": "banana", "color": "yellow"},
    {"name": "grape", "color": "purple"},
    {"name": "orange", "color": "orange"},
    {"name": "kiwi", "color": "green"},
    {"name": "strawberry", "color": "red"}
]

color_dict = defaultdict(list)

for fruit in fruits:
    color_dict[fruit["color"]].append(fruit["name"])

print(color_dict)

Output

defaultdict(, {'red': ['apple', 'strawberry'], 'yellow': ['banana'], 'purple': ['grape'], 'orange': ['orange'], 'green': ['kiwi']})

Method 3: Using itertools.groupby

We can work with iterators by using the itertools module of Python. The groupby function allows us to group items based on a specific criterion. Let's consider an example where we have a list of words, and we want to group them based on their first letter.

Syntax

list_name.append(element)

Here, the append() function is a list method used to add an element to the end of the list_name. It modifies the original list by adding the specified element as a new item.

itertools.groupby(iterable, key=None)

Here, iterable is any collection of element on which groupby() is applied. The key is optional parameter and it is a function that is used as a key for grouping.

Example

In the below example, we use the key_func lambda function to extract the first letter of each word. We sort the list of words to ensure that similar items are grouped together. The groupby function returns an iterator that provides consecutive keys and groups based on the key_func. We convert the group iterator to a list and assign it as the value for each key in the "word_dict".

import itertools

words = ["apple", "banana", "cat", "dog", "elephant", "ant"]

key_func = lambda x: x[0]
words.sort()

word_dict = {}

for key, group in itertools.groupby(words, key_func):
    word_dict[key] = list(group)

print(word_dict)

Output

{'a': ['ant', 'apple'], 'b': ['banana'], 'c': ['cat'], 'd': ['dog'], 'e': ['elephant']}

Conclusion

In this article, we discussed how we can group similar items to a dictionary values list using various methods in Python. We started with a simple for loop, then utilized the defaultdict class from the collections module, and finally employed the groupby function from the itertools module. Each method has its advantages and can be chosen based on the specific requirements of the task at hand.

Updated on: 19-Jul-2023

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements