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
Python - Inter Matrix Grouping
Inter Matrix Grouping is a technique where elements from two lists are grouped based on matching first elements. When matches are found, the first element becomes a dictionary key, and the remaining elements form grouped values. Python provides several built-in functions like setdefault(), defaultdict(), and append() to implement this efficiently.
Understanding the Concept
Let's understand this with an example:
list1 = [[1, 2], [3, 4], [4, 5], [5, 6]]
list2 = [[5, 60], [1, 22], [4, 59], [3, 14]]
# Result: {1: [2, 22], 3: [4, 14], 4: [5, 59], 5: [6, 60]}
Here, keys 1, 3, 4, 5 appear in both lists, so their corresponding values are grouped together.
Method 1: Using setdefault() Function
The setdefault() method returns a key's value if it exists, or creates the key with a default value if it doesn't ?
def inter_matrix_grouping(list1, list2):
result = {}
for key, value in list1 + list2:
result.setdefault(key, []).append(value)
return result
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
result = inter_matrix_grouping(list1, list2)
print("Inter Matrix grouping result:")
print(result)
Inter Matrix grouping result:
{20: [0, 100], 40: [200, 500], 60: [300, 50]}
Method 2: Using defaultdict() Function
The defaultdict() automatically creates missing keys with default values, eliminating the need to check if a key exists ?
from collections import defaultdict
def inter_matrix_grouping(list1, list2):
result = defaultdict(list)
for key, value in list1 + list2:
result[key].append(value)
return dict(result)
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
result = inter_matrix_grouping(list1, list2)
print("Inter Matrix grouping result:")
print(result)
Inter Matrix grouping result:
{20: [0, 100], 40: [200, 500], 60: [300, 50]}
Method 3: Using Manual Dictionary Operations
This approach manually checks if keys exist and creates lists accordingly ?
def inter_matrix_grouping(list1, list2):
merged_list = list1 + list2
result = {}
for key, value in merged_list:
if key in result:
result[key].append(value)
else:
result[key] = [value]
return result
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
result = inter_matrix_grouping(list1, list2)
print("The result of Inter Matrix Grouping:")
print(result)
The result of Inter Matrix Grouping:
{20: [0, 100], 40: [200, 500], 60: [300, 50]}
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
setdefault() |
Good | Fast | Simple grouping tasks |
defaultdict() |
Excellent | Fastest | Complex grouping operations |
| Manual Check | Fair | Slower | Learning purposes |
Conclusion
Inter Matrix Grouping efficiently combines data from multiple lists based on common keys. The defaultdict() approach offers the best performance and readability, while setdefault() provides a good balance for simpler use cases.
