my name jeff
my name jeff

Reputation: 99

Python - Remove nested lists in dictionary

I have a dict named results.

the dict is structured in this format:

{'a': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']], 'b': [['2', '2', '4'],['2', '2', '2'],['1', '2', '4']], 'c': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']]}

I wish to remove the duplicate nested lists for each key, therefore leaving the dict with:

{'a': [['1', '2', '4'],['1', '2', '2']], 'b': [['2', '2', '4'],['2', '2', '2'],['1', '2', '4']], 'c': [['1', '2', '4'],['1', '2', '2']]}

I have tried:

newdict = {}
for k, v in results.items():
    for i in v:
        if i not in i:
            newdict[k] = i

any help? thanks in advance!

Upvotes: 2

Views: 686

Answers (2)

Austin
Austin

Reputation: 26039

In case if order is important, you can use something like this:

results = {'a': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']],
           'b': [['2', '2', '4'],['2', '2', '2'],['1', '2', '4']],
           'c': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']]}

print({k: [y for x, y in enumerate(v) \
     if y not in v[:x]] for k, v in results.items()})

Output:

 {'a': [['1', '2', '4'], ['1', '2', '2']], 
  'b': [['2', '2', '4'], ['2', '2', '2'], ['1', '2', '4']], 
  'c': [['1', '2', '4'], ['1', '2', '2']]} 

To skip first sub-list and require checking only in the remaining, you could do:

print({k: [y for x, y in enumerate(v) \
     if y not in v[1:x]] for k, v in results.items()})

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140186

Your code is wrong beyond repair (sorry), mostly because of those 2 lines:

 if i not in i:  # makes no sense testing if something is inside itself
      newdict[k] = i   # overwrites the key with one list

You'd have to count each list, and only keep one occurrence of each.

If order doesn't matter you could do that with a nested dict/set/list comprehension.

results = {'a': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']], 'b': [['2', '2', '4'],['2', '2', '2'],['1', '2', '4']], 'c': [['1', '2', '4'],['1', '2', '2'],['1', '2', '2']]}

newdict = {k:[list(y) for y in {tuple(x) for x in v}] for k,v in results.items()}

print(newdict)

result:

{'a': [['1', '2', '2'], ['1', '2', '4']], 'b': [['2', '2', '4'], ['1', '2', '4'], ['2', '2', '2']], 'c': [['1', '2', '2'], ['1', '2', '4']]}

using a set allows to keep unicity, but you cannot put a list in a set, so the expression converts to tuple first (which is hashable), and converts back to list once the processing is done.

Upvotes: 3

Related Questions