We are given a list of items and our task is to group similar elements together as dictionary values. The keys will be the unique items and their values will be lists containing all occurrences of that item. For example, given ['apple', 'banana', 'apple', 'orange', 'banana'], the output should be: {'apple': ['apple', 'apple'], 'banana': ['banana', 'banana'], 'orange': ['orange']}
Using defaultdict
We use collections.defaultdict(list), which allows us to append values without checking if the key exists, this makes the solution clean and efficient.
from collections import defaultdict
a = ['apple', 'banana', 'apple', 'orange', 'banana']
d = defaultdict(list)
for item in a:
d[item].append(item)
print(dict(d))
Output
{'apple': ['apple', 'apple'], 'banana': ['banana', 'banana'], 'orange': ['orange']}
Explanation:
- defaultdict(list) initializes a dictionary where missing keys default to empty lists.
- The loop iterates through a, appending each item to its corresponding key in d.
- dict(d) converts defaultdict to a regular dictionary before printing.
Table of Content
Using setdefault()
We use Python’s built-in setdefault() to initialize lists only when needed avoiding manual key checks.
a = ['apple', 'banana', 'apple', 'orange', 'banana']
d = {}
for item in a:
d.setdefault(item, []).append(item)
print(d)
Output
{'apple': ['apple', 'apple'], 'banana': ['banana', 'banana'], 'orange': ['orange']}
Explanation:
- setdefault(item, []) initializes item with an empty list if it’s not already present.
- .append(item) adds the item to its respective list.
- This avoids unnecessary if checks and keeps the code clean.
Using groupby() from itertools
We use itertools.groupby(), which groups consecutive similar elements, this method requires sorting the list first.
from itertools import groupby
a = ['apple', 'banana', 'apple', 'orange', 'banana']
a.sort() # Sorting is required for groupby to work correctly
d = {k: list(g) for k, g in groupby(a)}
print(d)
Output
{'apple': ['apple', 'apple'], 'banana': ['banana', 'banana'], 'orange': ['orange']}
Explanation:
- a.sort() ensures similar elements are adjacent, as groupby() only works on consecutive items.
- groupby(a) creates groups for each unique item.
- {k: list(g) for k, g in groupby(a)} constructs the dictionary.
Using Dictionary Comprehension with count()
We use dictionary comprehension and .count() to create the groups, this method is concise but inefficient for large lists.
a = ['apple', 'banana', 'apple', 'orange', 'banana']
d = {item: [item] * a.count(item) for item in set(a)}
print(d)
Output
{'banana': ['banana', 'banana'], 'orange': ['orange'], 'apple': ['apple', 'apple']}
Explanation:
- set(a) ensures we only iterate unique elements.
- a.count(item) counts occurrences of each item in a.
- [item] * a.count(item) creates a list with the item repeated count(item) times.