Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep duplicates by key in a list of dictionaries

I have a list of dictionaries, and I would like to obtain those that have the same value in a key:

my_list_of_dicts = [{
    'id': 3,
    'name': 'John'
  },{
    'id': 5,
    'name': 'Peter'
  },{
    'id': 2,
    'name': 'Peter'
  },{
    'id': 6,
    'name': 'Mariah'
  },{
    'id': 7,
    'name': 'John'
  },{
    'id': 1,
    'name': 'Louis'
  }
]

I want to keep those items that have the same 'name', so, I would like to obtain something like:

duplicates: [{
    'id': 3,
    'name': 'John'
  },{
    'id': 5,
    'name': 'Peter'
  },{
    'id': 2,
    'name': 'Peter'
  }, {
    'id': 7,
    'name': 'John'
  }
]

I'm trying (not successfully):

duplicates = [item for item in my_list_of_dicts if len(my_list_of_dicts.get('name', None)) > 1]

I have clear my problem with this code, but not able to do the right sentence

like image 683
cucuru Avatar asked Jan 20 '20 12:01

cucuru


2 Answers

Another concise way using collections.Counter:

from collections import Counter

my_list_of_dicts = [{
    'id': 3,
    'name': 'John'
  },{
    'id': 5,
    'name': 'Peter'
  },{
    'id': 2,
    'name': 'Peter'
  },{
    'id': 6,
    'name': 'Mariah'
  },{
    'id': 7,
    'name': 'John'
  },{
    'id': 1,
    'name': 'Louis'
  }
]

c = Counter(x['name'] for x in my_list_of_dicts)

duplicates = [x for x in my_list_of_dicts if c[x['name']] > 1]
like image 82
Austin Avatar answered Oct 05 '22 12:10

Austin


You could use the following list comprehension:

>>> [d for d in my_list_of_dicts if len([e for e in my_list_of_dicts if e['name'] == d['name']]) > 1]
[{'id': 3, 'name': 'John'},
 {'id': 5, 'name': 'Peter'},
 {'id': 2, 'name': 'Peter'},
 {'id': 7, 'name': 'John'}]
like image 43
CDJB Avatar answered Oct 05 '22 13:10

CDJB