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 - Uncommon elements in Lists of List
In this article, we will learn various methods to find uncommon elements in lists of lists ? elements that exist in one list but not in the other. This is a common requirement in data analysis when comparing nested data structures.
Understanding the Problem
Consider two lists of lists where we want to find sublists that appear in one list but not the other ?
list1 = [[1, 2], [3, 4], [5, 6]] list2 = [[3, 4], [5, 6], [7, 8]] # Expected output: [[1, 2], [7, 8]] # [1, 2] exists only in list1 # [7, 8] exists only in list2
Method 1: Using Nested Loops
The most straightforward approach uses nested loops to check each sublist against the other list ?
list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
uncommon_elements = []
# Check list1 sublists not in list2
for sublist in list1:
if sublist not in list2:
uncommon_elements.append(sublist)
# Check list2 sublists not in list1
for sublist in list2:
if sublist not in list1:
uncommon_elements.append(sublist)
print("Uncommon elements:", uncommon_elements)
Uncommon elements: [[1, 2], [7, 8]]
Method 2: Using List Comprehension
A more Pythonic approach using list comprehension to filter uncommon elements ?
list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
uncommon_elements = [
sublist for sublist in list1 + list2
if sublist not in (list1 if sublist in list2 else list2)
]
print("Uncommon elements:", uncommon_elements)
Uncommon elements: [[1, 2], [7, 8]]
Method 3: Using Set Operations
Convert sublists to tuples for set operations, then find the symmetric difference ?
list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
set1 = set(map(tuple, list1))
set2 = set(map(tuple, list2))
# Find symmetric difference and convert back to lists
uncommon_tuples = set1.symmetric_difference(set2)
uncommon_elements = [list(t) for t in uncommon_tuples]
print("Uncommon elements:", uncommon_elements)
Uncommon elements: [[1, 2], [7, 8]]
Method 4: Using Pandas DataFrame
Leverage pandas for data manipulation when working with larger datasets ?
import pandas as pd
list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)
# Concatenate and drop duplicates
combined_df = pd.concat([df1, df2])
uncommon_df = combined_df.drop_duplicates(keep=False)
uncommon_elements = uncommon_df.values.tolist()
print("Uncommon elements:", uncommon_elements)
Uncommon elements: [[1, 2], [7, 8]]
Performance Comparison
| Method | Time Complexity | Memory Usage | Best For |
|---|---|---|---|
| Nested Loops | O(n²) | Low | Small datasets |
| List Comprehension | O(n²) | Medium | Readable code |
| Set Operations | O(n) | Medium | Performance |
| Pandas | O(n) | High | Large datasets |
Conclusion
Set operations provide the most efficient solution for finding uncommon elements in lists of lists. Use pandas for complex data manipulation scenarios, and nested loops for simple cases where readability is prioritized over performance.
