Python | Count unique sublists within list
Last Updated :
25 Apr, 2023
Given a list of lists, the task is to find the count of unique sublists within list. Examples:
Input: [['Geek', 'for', 'geeks'], ['geeks', 'for'],
['for', 'Geeks', 'geek'], ['Geek', 'for', 'geeks']]
Output:
{('geeks', 'for'): 1, ('for', 'Geeks', 'geek'): 1,
('Geek', 'for', 'geeks'): 2}
Below are some ways to achieve the task. Method #1: Using Iteration
Python3
# Python code to count unique sublist within list
# Input list initialization
Input = [['Geek', 'for', 'geeks'], ['geeks', 'for'],
['for', 'Geeks', 'geek'], ['Geek', 'for', 'geeks']]
# Output list initialization
Output = {}
# Using Iteration
for lis in Input:
Output.setdefault(tuple(lis), list()).append(1)
for a, b in Output.items():
Output[a] = sum(b)
# Printing output
print(Output)
Output:
{('Geek', 'for', 'geeks'): 2, ('geeks', 'for'): 1, ('for', 'Geeks', 'geek'): 1}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using Counter
Python3
# Python code to find count of unique list in list of list
# Importing counter from collection
from collections import Counter
# Input list initialization
lst = [[1, 2, 3], [4, 5, 6], [3, 2, 1], [1, 2, 3]]
# Using counter
Output = Counter([tuple(i) for i in lst])
# Printing output
print(Output)
Output:Counter({(1, 2, 3): 2, (3, 2, 1): 1, (4, 5, 6): 1})
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant space needed
Method #3: Using Pandas
Python3
# Python code to count unique sublist within list
# Importing
from collections import Counter
import pandas as pd
# Input list initialization
lst = [[1, 2, 3], [4, 5, 6], [3, 2, 1], [1, 2, 3]]
# Getting count
dict = Counter([tuple(i) for i in lst])
# Creating pandas dataframe
Output = pd.DataFrame(data ={'list': list(dict.keys()),
'count': list(dict.values())})
# Printing output
print(Output)
Output:count list
0 1 (3, 2, 1)
1 1 (4, 5, 6)
2 2 (1, 2, 3)
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Similar Reads
Count unique items in a list There are several methods for finding or counting unique items inside a list in Python. Here we'll discuss 3 methods.Brute Force ApproachThe first method is the brute force approach.We traverse from the start and check the items. If the item is not in the empty list(as it has taken empty) then we wi
3 min read
Python | Unique pairs in list Sometimes, while working with python list, we can have a binary matrix ( Nested list having 2 elements ). And we can have a problem in which we need to find the uniqueness of a pair. A pair is unique irrespective of order, it doesn't appear again in list. Let's discuss certain way in which this task
6 min read
Python - Test for Unique Frequencies Given a list, find if frequencies of each element of values are in itself unique values. Input : test_list = [4, 3, 2] Output : False Explanation : All have 1 as frequency, hence duplicacy, hence False Input : test_list = [4, 3, 3, 2, 2, 2] Output : True Explanation : 1, 2, 3 are frequencies of 4, 3
7 min read
Count the Sublists Containing given Element in a List - Python Given a list of lists our task is to count the number of sublists containing the given element x. For example: li = [[1, 3, 5], [1, 3, 5, 7], [1, 3, 5, 7, 9]] and x = 1 then output will be 3.Using List ComprehensionThis method uses list comprehension to check for the presence of the given element in
2 min read
Python | List Element Count with Order Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
4 min read
Python - Count frequency of Sublist in given list Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
3 min read