Python Merging duplicates to list of list (original) (raw)

Last Updated : 23 Jan, 2025

We are given a list we need to merge the duplicate to lists of list. **For example a list a=[1,2,2,3,4,4,4,5] we need to merge all the duplicates in lists so that the output should be [[2,2],[4,4,4]]. This can be done by using multiple functions like defaultdict from collections and Counter and various approaches with combinations of loop

Using collections.defaultdict

We can use defaultdict from collections can be used to group duplicates together efficiently.

Python `

from collections import defaultdict

a = [1, 2, 2, 3, 4, 4, 4, 5]

Initialize defaultdict with list as default factory

b = defaultdict(list)

Group duplicates together in lists

for item in a: b[item].append(item)

Get the lists of duplicates

c = [group for group in b.values() if len(group) > 1]

print("Merged List:", c)

`

Output

Merged List: [[2, 2], [4, 4, 4]]

**Explanation:

Using a Manual Approach

A more manual approach where we can iterate through the list and merge duplicates.

Python `

a = [1, 2, 2, 3, 4, 4, 4, 5]

Initialize an empty list to store merged lists

m = []

Iterate over the list and merge duplicates

while a: current = a[0] duplicates = [current]

# Find all occurrences of the current item
a = [item for item in a[1:] if item != current]  # Remove the current item
m.append(duplicates + [item for item in a if item == current])

print("Merged List:", m)

`

Output

Merged List: [[1], [2], [3], [4], [5]]

**Explanation:

Using collections.Counter

We can use Counter to count occurrences and then create lists of duplicates.

Python `

from collections import Counter

a = [1, 2, 2, 3, 4, 4, 4, 5]

Count occurrences of each element

c = Counter(a)

Create lists of duplicates

d = [[key] * c[key] for key in c if c[key] > 1]

print("Merged List:", d)

`

Output

Merged List: [[2, 2], [4, 4, 4]]

**Explanation:

Similar Reads