Open In App

Python | Remove unidentical lists

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a binary matrix ( Nested list having 2 elements ). And we can have a problem in which we need to remove unidentical lists. A pair is unique irrespective of order, it doesn’t appear again in list. Let’s discuss certain way in which this task can be performed. 

Method : Using frozenset() + Counter() + list comprehension 

The combination of above functions can perform this task. The frozenset() is used for ignoring the ordering, Counter() is used to perform the task of checking the uniqueness and iteration is done using list comprehension.

Python3




# Python3 code to demonstrate working of
# Remove unidentical lists
# using frozenset() + Counter() + list comprehension
from collections import Counter
 
# initialize list
test_list = [[5, 6], [9, 8], [8, 9], [1, 4], [6, 5], [10, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove unidentical lists
# using frozenset() + Counter() + list comprehension
temp = Counter(frozenset(ele) for ele in test_list)
res = [ele for ele in test_list if temp[frozenset(ele)] >= 2]
 
# printing result
print("The list after removal of unidentical lists : " + str(res))


Output : 

The original list is : [[5, 6], [9, 8], [8, 9], [1, 4], [6, 5], [10, 1]]
The list after removal of unidentical lists : [[5, 6], [9, 8], [8, 9], [6, 5]]

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

Method #2: Using loop:

Algorithm:

  1. Initialize an empty list res.
  2. Loop through each element in the test_list.
  3. For each element, loop through all the subsequent elements in the test_list.
  4. Check if the set of the current element is equal to the set of the subsequent element.
  5. If they are equal, add both elements to the res list and break out of the inner loop.
  6. Convert the list of tuples to a set to remove duplicates, and then convert it back to a list.
  7. Print the original list and the resulting list.

Python3




# initialize list
test_list = [[5, 6], [9, 8], [8, 9], [1, 4], [6, 5], [10, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove unidentical lists
res = []
 
for i in range(len(test_list)):
    for j in range(i+1, len(test_list)):
 
        if set(test_list[i]) == set(test_list[j]):
            res.append(test_list[i])
            res.append(test_list[j])
            break
 
# printing result
print("The list after removal of unidentical lists : " +
      str(list(set(map(tuple, res)))))
 
# This code is contributed by Jyothi pinjala.


Output

The original list is : [[5, 6], [9, 8], [8, 9], [1, 4], [6, 5], [10, 1]]
The list after removal of unidentical lists : [(5, 6), (9, 8), (8, 9), (6, 5)]

Time Complexity: O(n^2), where n is the length of the input list. This is because there are two nested loops that iterate through the entire list.

Auxiliary Space: O(n), where n is the length of the input list. This is because we are using a list res to store the matching elements, which can have a maximum size of n.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads