Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - remove duplicate of dict that contains list of lists

Tags:

python

list

suppose training_data is a dict that contains a list of lists such as {1: [[1, 2], [1, 3], [1, 2, 5]], 2: [[1], [5], [1, 6]], 3: [[7], [5]]}

I want to merge each value in training_data and then flatten it once to a list so that it will become [[1,2,3,5],[1,5,6],[7,5]].

like image 243
SXKDZ Avatar asked Dec 07 '25 20:12

SXKDZ


2 Answers

you can achieve the same result with a one-line comprehension, chaining and using a set + sort

import itertools

d = {1: [[1, 2], [1, 3], [1, 2, 5]], 2: [[1], [5], [1, 6]], 3: [[7], [5]]}

result = [sorted(set(itertools.chain.from_iterable(v))) for v in d.values()]

print(result)

outputs:

[[1, 2, 3, 5], [1, 5, 6], [5, 7]]
like image 90
Jean-François Fabre Avatar answered Dec 10 '25 08:12

Jean-François Fabre


If you only need unique elements from the list without maintaining the order of elements in initial list, then you may use set here with itertools.chain.from_iterable. You may use these with list comprehension to achieve your desired list as:

>>> from itertools import chain
>>> my_dict = {1: [[1, 2], [1, 3], [1, 2, 5]], 2: [[1], [5], [1, 6]], 3: [[7], [5]]}

>>> [list(set(chain.from_iterable(v))) for v in my_dict.values()]
[[1, 2, 3, 5], [1, 5, 6], [5, 7]]

Note: since dictionaries are unordered in Python <3.6, the order of sub-lists in the resultant lists might differ.

For preserving the order of elements in the sublist while merging, take a look at: Pythonic way to merge two overlapping lists, preserving order

like image 43
Moinuddin Quadri Avatar answered Dec 10 '25 10:12

Moinuddin Quadri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!