Open In App

Python | Uncommon elements in Lists of List

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This particular article aims at achieving the task of finding uncommon two list, in which each element is in itself a list. This is also a useful utility as this kind of task can come in life of programmer if he is in the world of development. Lets discuss some ways to achieve this task.

Method 1 : Naive Method 
This is the simplest method to achieve this task and uses the brute force approach of executing a loop and to check if one list contains similar list as of the other list, not including that. 

Python3




# Python 3 code to demonstrate
# Uncommon elements in List
# using naive method
 
# initializing lists
test_list1 = [ [1, 2], [3, 4], [5, 6] ]
test_list2 = [ [3, 4], [5, 7], [1, 2] ]
 
# printing both lists
print ("The original list 1 : " + str(test_list1))
print ("The original list 2 : " + str(test_list2))
 
# using naive method
# Uncommon elements in List
res_list = []
for i in test_list1:
    if i not in test_list2:
        res_list.append(i)
for i in test_list2:
    if i not in test_list1:
        res_list.append(i)
         
# printing the uncommon
print ("The uncommon of two lists is : " + str(res_list))


Output : 

The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The uncommon of two lists is : [[5, 6], [5, 7]]

 

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

 Method 2 : Using set() + map() and ^ 
The most efficient and recommended method to perform this task is using the combination of set() and map() to achieve it. Firstly converting inner lists to tuples using map, and outer lists to set, use of ^ operator can perform the set symmetric difference and hence perform this task. Further if it is required to get in lists of list fashion, we can convert outer and inner containers back to list using map().

Python3




# Python 3 code to demonstrate
# Uncommon elements in Lists of List
# using map() + set() + ^
 
# initializing lists
test_list1 = [ [1, 2], [3, 4], [5, 6] ]
test_list2 = [ [3, 4], [5, 7], [1, 2] ]
 
# printing both lists
print ("The original list 1 : " + str(test_list1))
print ("The original list 2 : " + str(test_list2))
 
# using map() + set() + ^
# Uncommon elements in Lists of List
res_set = set(map(tuple, test_list1)) ^ set(map(tuple, test_list2))
res_list = list(map(list, res_set))
 
# printing the uncommon
print ("The uncommon of two lists is : " + str(res_list))


Output : 

The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The uncommon of two lists is : [[5, 6], [5, 7]]

 

Time Complexity: O(n*n) where n is the number of elements in the string list. The set() + map() and ^  is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method 3 : Using list comprehension and set intersection:

Algorithm:

  1. Initialize two lists, test_list1 and test_list2
  2. Initialize an empty list res_list to store the uncommon elements
  3. Use a list comprehension to iterate through each element in test_list1, and check if it is not in test_list2. If it is not, append it to res_list
  4. Use another list comprehension to iterate through each element in test_list2, and check if it is not in test_list1.
  5. If it is not, append it to res_list
  6. Print the res_list

Python3




# initializing lists
test_list1 = [[1, 2], [3, 4], [5, 6]]
test_list2 = [[3, 4], [5, 7], [1, 2]]
# printing both lists
print ("The original list 1 : " + str(test_list1))
print ("The original list 2 : " + str(test_list2))
res_list = [x for x in test_list1 if x not in test_list2] + [y for y in test_list2 if y not in test_list1]
# printing the uncommon
print("The uncommon of two lists is : " + str(res_list))
 
#This code  is contributed by Jyothi pinjala


Output

The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The uncommon of two lists is : [[5, 6], [5, 7]]

Time Complexity:
The time complexity of this code is O(n^2), where n is the length of the longer list. This is because the ‘not in’ operation requires iterating through the entire list to check if the element is present.

Space Complexity:
The space complexity of this code is also O(n^2), since we are creating a new list to store the uncommon elements, and this list could potentially be as large as the input lists.



Similar Reads

Python Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
3 min read
Concatenated string with uncommon characters in Python
Two strings are given and you have to modify the 1st string such that all the common characters of the 2nd string have to be removed and the uncommon characters of the 2nd string have to be concatenated with the uncommon characters of the 1st string. Examples: Input : S1 = "aacdb", S2 = "gafd"Output : "cbgf"Input : S1 = "abcs";, S2 = "cxzca";Output
4 min read
Python program to find uncommon words from two Strings
Given two sentences as strings A and B. The task is to return a list of all uncommon words. A word is uncommon if it appears exactly once in any one of the sentences, and does not appear in the other sentence. Note: A sentence is a string of space-separated words. Each word consists only of lowercase letters. Examples: Input : A = "Geeks for Geeks"
4 min read
Python - String uncommon characters
One of the string operation can be computing the uncommon characters of two strings i.e, output the uncommon values that appear in both strings. This article deals with computing the same in different ways. Method 1: Using set() + symmetric_difference() Set in python usually can perform the task of performing set operations such as set symmetric di
5 min read
Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers. Examples: Input: A = 378212, B = 78124590Output: 9 5 4 3 0Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers. Inpu
5 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Python | Sorting list of lists with similar list elements
Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Let's discuss certain ways in which this can be perf
5 min read
Python | Merge List with common elements in a List of Lists
Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27, 55], [43, 0, 22], [43, 0, 96], [55, 27, 11]] Out
3 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Practice Tags :