Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create Python dictionary with duplicate keys?
In Python, a dictionary doesn't allow duplicate keys. However, we can work around this limitation using defaultdict from the Collections module to store multiple values for the same key in the form of lists.
Understanding defaultdict
The defaultdict is a subclass of the built-in dict class that provides a default value for keys that don't exist. When you access a missing key, it automatically creates that key with a default value using a default factory function.
from collections import defaultdict # Create defaultdict with list as default factory d = defaultdict(list) print(type(d)) print(d['new_key']) # Automatically creates empty list
<class 'collections.defaultdict'> []
Creating Dictionary with Duplicate Keys
Here's how to handle duplicate keys by storing multiple values in lists ?
from collections import defaultdict
# Create defaultdict with list as default factory
data_dict = defaultdict(list)
# Sample data with duplicate keys
key_value_pairs = [(1, 111), (2, 222), (3, 333), (1, 'aaa'), (2, 'bbb'), (3, 'ccc')]
# Add values to defaultdict
for key, value in key_value_pairs:
data_dict[key].append(value)
# Convert to regular dictionary
result = dict(data_dict)
print(result)
{1: [111, 'aaa'], 2: [222, 'bbb'], 3: [333, 'ccc']}
Alternative Approaches
Using Regular Dictionary with Manual Checking
# Manual approach without defaultdict
data_dict = {}
key_value_pairs = [(1, 111), (2, 222), (1, 'aaa'), (2, 'bbb')]
for key, value in key_value_pairs:
if key in data_dict:
data_dict[key].append(value)
else:
data_dict[key] = [value]
print(data_dict)
{1: [111, 'aaa'], 2: [222, 'bbb']}
Using setdefault() Method
# Using setdefault method
data_dict = {}
key_value_pairs = [(1, 111), (2, 222), (1, 'aaa'), (2, 'bbb')]
for key, value in key_value_pairs:
data_dict.setdefault(key, []).append(value)
print(data_dict)
{1: [111, 'aaa'], 2: [222, 'bbb']}
Comparison
| Method | Code Simplicity | Performance | Best For |
|---|---|---|---|
defaultdict |
High | Fast | Multiple duplicate keys |
| Manual checking | Medium | Slower | Simple cases |
setdefault() |
Medium | Medium | One-liner solution |
Conclusion
Use defaultdict(list) for handling duplicate keys efficiently. It automatically creates empty lists for new keys and simplifies code when dealing with multiple values per key.
Advertisements
