Python - Uncommon elements in Lists of List


In this article we will learn about various methods using which we can find the uncommon element in the list of lists that means the element which is not present in the other list. When working with lists of lists in Python, it's common to encounter scenarios where you need to find the uncommon elements present in these nested structures. Finding the uncommon elements in lists of lists can be achieved using various methods and techniques. In this article, we will explore 8 examples and methods to help you easily identify the uncommon elements in your lists of lists.

Example

Let’s understand using an example −

list1 = [[1, 2], [3, 4], [5, 6]] 
list2 = [[3, 4], [5, 6], [7, 8]]

Output

[1, 2, 7, 8]

So you can observe we have given two list of lists and we will compare both the list and check which element is not present in the oher one so element [1,2] from list1 is not there in the list2 and element [7,8] from list2 is not there in the list1 so output will come as [1,2,7,8].

Method 1: Using Nested Loops

This is very basic method in which we will use the nested loop to find the uncommon element in the lists of lists.

Example

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
uncommon_elements = []
for subl1 in list1:
   if subl1 not in list2:
      uncommon_elements.append(subl1)
for subl2 in list2:
   if subl2 not in list1:
      uncommon_elements.append(subl2)
print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :",uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [[1, 2], [7, 8]]

Explanation

Here in the above method we used two loops to traverse over the lists of lists and for each sublist we checked if it is present in the other list, and if not, we add it to the uncommon_elements list.

Method 2: Using List Comprehension

In this method we will use List comprehension technique using which we can filter the element in lists of lists. We will use nested loops for traversing over sublists of both the lists and we will find the uncommon element.

Example

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
uncommon_elements = [
   element for sublist in list1 + list2
   for element in sublist
   if (sublist not in list1) or (sublist not in list2)
]

print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :",uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [1, 2, 7, 8]

Explanation

Here in the above example, we will first concatenate both the lists into a single list then we use the list comprehension to traverse into each sublist and we check if it is present in either list1 or list2, but not in both. Then we collect the uncommon element from the list.

Method 3: Using Set Difference

In this method we will use the concept of set and we will calculate the difference between the two sets to find the uncommon lists elements.

Example

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]

set1 = set(map(tuple, list1))
set2 = set(map(tuple, list2))
uncommon_elements = list(map(list, set1 - set2)) + list(map(list, set2 - set1))

print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :",uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [[1, 2], [7, 8]]

Explanation

Here in the above example, we calculated the difference between set1 and set2 and vice versa. Then we collected the element which is present in any set but in the other will be uncommon elements.

Method 4: Using Pandas DataFrame

In this method we will use the concept of pandas dataframe library which is very popular for data manipulation. We will use the dataframe object provided by the pandas dataframe to find uncommon elements in lists of lists from two different lists.

Example

import pandas as pd
list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]
df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)
uncommon_elements = pd.concat([df1, df2]).drop_duplicates(keep=False).values.tolist()

print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :",uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [[1, 2], [7, 8]]

Explanation

Here in the above example we created two pandas DataFrames from list1 and list2. Then we concatenate both the data frames and we drop the duplicate rows of the dataframe which give us results as only the uncommon elements.

Method 5: Using Set Intersection and Difference

In this method we will use the concept of set intersection and difference to find uncommon elements in the lists of lists.

Example

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]

set1 = set(map(tuple, list1))
set2 = set(map(tuple, list2))
uncommon_elements = list(map(list, (set1 - set2).union(set2 - set1)))

print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :", uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [[1, 2], [7, 8]]

Explanation

Here in the above example we convert both list1 and list2 into sets of tuples. Then we calculated the difference between these sets. The union operation combines the differences to give us the uncommon elements as tuples, which are then converted back into lists.

Method 6: Using Sets and Symmetric Difference

In this method we will use the concept of set and Symmetric difference to find uncommon elements in the lists of lists.

Example

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [[3, 4], [5, 6], [7, 8]]

set1 = set(map(tuple, list1))
set2 = set(map(tuple, list2))
uncommon_elements = list(map(list, set1.symmetric_difference(set2)))

print("list1:", list1)
print("list2:", list2)
print("Uncommon elements from the lists of lists :",uncommon_elements)

Output

list1: [[1, 2], [3, 4], [5, 6]] 
list2: [[3, 4], [5, 6], [7, 8]] 
Uncommon elements from the lists of lists : [[1, 2], [7, 8]]

Explanation

Here in the above example we first convert sublists of both list1 and list2 into sets of tuples. We will use the symmetric difference between both sets which will give us results as uncommon elements in the form of tuples. We then convert these tuples back into lists to get the final uncommon_elements list.

Conclusion

So, we learned that finding uncommon elements in lists of lists from two different lists is very crucial task in data analysis and manipulation. We saw different methods including basic, pandas, lists and symmetric differences using which we can calculate the uncommon element in lists of lists. You can use any of the methods which best suits your requirement but having knowledge is good for the learning purpose.

Updated on: 06-Oct-2023

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements