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 - Merge two List of Lists According to First Element
When working with lists of lists in Python, you often need to merge them based on their first element. Python provides several efficient approaches to accomplish this task using built-in functions like sorted(), dictionaries, and the itertools.groupby() function.
Why Merge Lists by First Element?
Merging lists of lists according to the first element is useful for:
Data Organization: Group related data items that share a common identifier.
Data Processing: Combine datasets from multiple sources based on a key field.
Performance: Python's built-in functions provide optimized solutions for these operations.
Method 1: Sorting and Merging
This approach combines both lists, sorts by the first element, and merges sublists with matching keys ?
def merge_by_sorting(list1, list2):
# Combine both lists
combined = list1 + list2
# Sort by first element of each sublist
sorted_list = sorted(combined, key=lambda x: x[0])
# Merge sublists with same first element
merged = [sorted_list[0]]
for sublist in sorted_list[1:]:
if sublist[0] == merged[-1][0]:
# Same first element, extend the last merged sublist
merged[-1].extend(sublist[1:])
else:
# Different first element, add as new sublist
merged.append(sublist)
return merged
# Example usage
list1 = [[1, 'a', 'b'], [3, 'x'], [1, 'c']]
list2 = [[2, 'j'], [1, 'd'], [3, 'y']]
result = merge_by_sorting(list1, list2)
print(result)
[[1, 'a', 'b', 'c', 'd'], [2, 'j'], [3, 'x', 'y']]
Method 2: Using Dictionary
This approach uses a dictionary to group elements by their first value ?
def merge_by_dictionary(list1, list2):
# Combine both lists
combined = list1 + list2
merged_dict = {}
# Group by first element using dictionary
for sublist in combined:
key = sublist[0]
values = sublist[1:]
if key in merged_dict:
merged_dict[key].extend(values)
else:
merged_dict[key] = values
# Convert back to list of lists
return [[key] + values for key, values in merged_dict.items()]
# Example usage
list1 = [[1, 'a'], [3, 'x'], [1, 'b']]
list2 = [[2, 'j'], [1, 'c'], [3, 'y']]
result = merge_by_dictionary(list1, list2)
print(result)
[[1, 'a', 'b', 'c'], [3, 'x', 'y'], [2, 'j']]
Method 3: Using itertools.groupby
This method uses itertools.groupby() to group consecutive elements with the same key ?
import itertools
def merge_by_groupby(list1, list2):
# Combine and sort by first element
combined = list1 + list2
sorted_list = sorted(combined, key=lambda x: x[0])
# Group by first element
grouped = itertools.groupby(sorted_list, key=lambda x: x[0])
merged = []
for key, group in grouped:
# Flatten all values for this key
all_values = []
for sublist in group:
all_values.extend(sublist[1:])
merged.append([key] + all_values)
return merged
# Example usage
list1 = [[1, 'a'], [3, 'x'], [1, 'b']]
list2 = [[2, 'j'], [1, 'c'], [3, 'y']]
result = merge_by_groupby(list1, list2)
print(result)
[[1, 'a', 'b', 'c'], [2, 'j'], [3, 'x', 'y']]
Comparison
| Method | Preserves Order | Time Complexity | Best For |
|---|---|---|---|
| Sorting + Merging | Sorted by key | O(n log n) | When you need sorted output |
| Dictionary | Insertion order | O(n) | Fastest for unsorted data |
| itertools.groupby | Sorted by key | O(n log n) | Clean readable code |
Conclusion
Use the dictionary approach for best performance when order doesn't matter. Use itertools.groupby() for clean, readable code when you need sorted output. The sorting method provides fine control over the merging process.
