Uncommon Elements in Lists of List - Python
Last Updated :
10 Feb, 2025
We are given two lists of lists and our task is to find the sublists that are uncommon between them. (a sublist is considered uncommon if it appears in only one of the lists and not in both.) For example: a = [[1, 2], [3, 4], [5, 6]] and b = [[3, 4], [5, 7], [1, 2]] then the output will be [[5, 6], [5, 7]]
Using Set Operations with Tuple Conversion
In this method we convert each sublist in "a" and "b" to a tuple so that they become hashable and can be added to sets and then compute the symmetric difference between these sets to find the sublists that appear only in one of them. Finally, we convert the resulting tuples back to lists. (Note: this method does not preserve the original order.)
Python
a = [[1, 2], [3, 4], [5, 6]]
b = [[3, 4], [5, 7], [1, 2]]
sa = set(tuple(x) for x in a) # Convert each sublist in 'a' to a tuple and create a set
sb = set(tuple(x) for x in b) # Convert each sublist in 'b' to a tuple and create a set
ut = sa.symmetric_difference(sb) # Get the symmetric difference between the two sets
res = [list(x) for x in ut] # Convert the tuples back to lists
print(res)
Explanation:
- Each sublist in a and b is converted into a tuple, making them hashable and allowing us to form sets (sa and sb) and then we compute the symmetric difference of sa and sb to find the tuples that are unique to either set.
- Resulting tuples are converted back to lists to obtain the final result res containing the uncommon sublists.
Using collections.Counter
In this method we are using collections.Counter to count the occurrences of each sublist from the combined lists. Since lists are unhashable hence we first convert each sublist to a tuple and then we extract only those tuples that appear exactly once (i.e. are uncommon) and convert them back to lists.
Python
from collections import Counter
a = [[1, 2], [3, 4], [5, 6]]
b = [[3, 4], [5, 7], [1, 2]]
c = Counter(tuple(x) for x in a + b) # Count each sublist (converted to tuple) from both lists
res = [list(x) for x in c if c[x] == 1] # Convert tuples back to lists if they appear only once
print(res)
Explanation: We iterate over the counter and select only those sublists (converted back to lists) with a count of 1, which represent the uncommon sublists.
Using List Comprehension
In this method we iterate over each list using list comprehension and pick the sublists that are not found in the other list, this approach preserves the order of the original lists.
Python
a = [[1, 2], [3, 4], [5, 6]]
b = [[3, 4], [5, 7], [1, 2]]
ua = [x for x in a if x not in b] # Get sublists from 'a' that are not in 'b'
ub = [x for x in b if x not in a] # Get sublists from 'b' that are not in 'a'
res = ua + ub # Combine the results
print(res)
Explanation:
- First list comprehension iterates over each sublist in a and includes it in "ua" only if it does not appear in b.
- similarly the second list comprehension gathers sublists from b that aren’t in a and finally we concatenate the two lists to get the final result.
Using filter and lambda
In this method we use the filter function combined with a lambda function to extract the sublists that do not appear in the other list.
Python
a = [[1, 2], [3, 4], [5, 6]]
b = [[3, 4], [5, 7], [1, 2]]
ua = list(filter(lambda x: x not in b, a)) # Filter sublists from 'a' that are not in 'b'
ub = list(filter(lambda x: x not in a, b)) # Filter sublists from 'b' that are not in 'a'
res = ua + ub # Combine the filtered sublists
print(res)
Explanation:
- lambda function checks for each sublist in a whether it is not in b (and vice versa for b).
- filter function applies this condition to generate filtered lists ua and ub.
- At last we combine ua and ub to obtain the final result containing the uncommon sublists.