Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Merging duplicates to list of list

Python | Merging duplicates to list of list

Sometimes, we need to perform the conventional task of grouping some like elements into a separate list and thus forming a list of lists. This can also help in counting and also get the sorted order of elements. Let’s discuss certain ways in which this can be done. 

Method #1: Using collections.Counter() This particular function can prove to be quite useful to perform this particular task as it counts the frequency of elements in the list and then we can pair them using the list comprehension. 

Python3




# Python3 code to demonstrate
# grouping like elements as list
# using collections.Counter()
import collections
 
# initializing list
test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using collections.Counter()
# grouping like elements as list
temp = collections.Counter(test_list)
res = [[i] * j for i, j in temp.items()]
 
# print result
print("The elements after grouping are : " + str(res))


Output

The original list : [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
The elements after grouping are : [[1, 1, 1], [3, 3, 3], [4, 4, 4], [2, 2]]

Time complexity: O(n) where n is the number of elements in the list
Auxiliary space: O(n) where n is the number of unique elements in the list after grouping.

Method #2 : Using itertools.groupby() This problem can easily be solved by traditional group functionality that is offered by Python via group by function, which groups the like elements as suggested by the ame. 

Python3




# Python3 code to demonstrate
# grouping like elements as list
# using itertools.groupby()
import itertools
 
# initializing list
test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itertools.groupby()
# grouping like elements as list
res = [list(i) for j, i in itertools.groupby(sorted(test_list))]
 
# print result
print("The elements after grouping are : " + str(res))


Output

The original list : [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
The elements after grouping are : [[1, 1, 1], [2, 2], [3, 3, 3], [4, 4, 4]]

Time complexity: O(nlogn) .
Auxiliary space: O(n). 

Method #3 : Using count() and * operator

Python3




# Python3 code to demonstrate
# grouping like elements as list
 
# initializing list
test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# printing original list
print("The original list : " + str(test_list))
x = list(set(test_list))
res = []
for i in x:
    res.append([i]*test_list.count(i))
 
# print result
print("The elements after grouping are : " + str(res))


Output

The original list : [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
The elements after grouping are : [[1, 1, 1], [2, 2], [3, 3, 3], [4, 4, 4]]

Time complexity: O(n^2)
Auxiliary space: O(n) (for the resulting list)

Method #4 : Using set() and * operator

The given code initializes a list called test_list with a list of integers. It then creates a new list called result using a list comprehension and the count() method.

The list comprehension loops through the elements in set(test_list), which creates a new set object containing all the unique elements in test_list. For each element in this set, the count() method is called on test_list with the element as an argument. This returns the number of occurrences of the element in test_list. The element is then added to a new sublist, which is repeated the number of times it appears in test_list using the * operator. For example, if the element is 3 and it appears 3 times in test_list, the sublist would be [3, 3, 3].

Python3




# Initialize the list
test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# Use a list comprehension and the count() method to create a list of lists
# containing the elements in the list repeated the number of times they appear in the list
res = [[x] * test_list.count(x) for x in set(test_list)]
 
# Print the result
print("The elements after grouping are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The elements after grouping are : [[1, 1, 1], [2, 2], [3, 3, 3], [4, 4, 4]]

Time complexity: O(n), because the list comprehension and the count() method both loop through the entire list once. 
Auxiliary Space: O(n), because the result list will have a length equal to the number of unique elements in the list, and each element in the result list is a sublist containing at least one element.

Method 5:  using operator.countOf() method

Python3




# Python3 code to demonstrate
# grouping like elements as list
import operator as op
# initializing list
test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# printing original list
print("The original list : " + str(test_list))
x = list(set(test_list))
res = []
for i in x:
    res.append([i] * op.countOf(test_list, i))
 
# print result
print("The elements after grouping are : " + str(res))


Output

The original list : [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
The elements after grouping are : [[1, 1, 1], [2, 2], [3, 3, 3], [4, 4, 4]]

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #6: Using a dictionary

Step-by-step approach:

  • Initialize an empty dictionary.
  • Loop through the elements of the given list.
  • If the element is not already a key in the dictionary, add it with a value of an empty list.
  • Append the element to the list associated with its key in the dictionary.
  • Return a list of the values in the dictionary.

Below is the implementation of the above approach:

Python3




test_list = [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using a dictionary
# grouping like elements as list
temp = {}
for elem in test_list:
    if elem not in temp:
        temp[elem] = []
    temp[elem].append(elem)
res = list(temp.values())
 
# print result
print("The elements after grouping are : " + str(res))


Output

The original list : [1, 3, 4, 2, 1, 3, 4, 2, 3, 4, 1]
The elements after grouping are : [[1, 1, 1], [3, 3, 3], [4, 4, 4], [2, 2]]

Time complexity: O(n), where n is the length of the given list.
Auxiliary space: O(n), to store the dictionary.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments