keep duplicates by key in a list of dictionaries keep duplicates by key in a list of dictionaries python-3.x python-3.x

keep duplicates by key in a list of dictionaries


Another concise way using collections.Counter:

from collections import Countermy_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]


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'}]


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'  }]df = pd.DataFrame(my_list_of_dicts)df[df.name.isin(df[df.name.duplicated()]['name'])].to_json(orient='records')