-1
d= {'Time': '1', 'Key':'val1'}
d1={'Time':'2', 'Key':'val1'}
d2={'Time':'3', 'Key':'val1'}
d3={'Time':'3', 'Key':'val2'}
d3={'Time':'8', 'Key':'val2'}}

How to print the output in this format:

output=[{Time': '1', 'Key':'val1'}, {'Time':'3', 'Key':'val2'}]

How do I remove the duplicate values by a single key, value combination and print the first occurrence of the combination. Thanks for you help.

Prab
  • 1
  • 1

2 Answers2

0
lst =  [
    {'Time': '1', 'Key':'val1'},
    {'Time':'2', 'Key':'val1'},
    {'Time':'3', 'Key':'val1'},
    {'Time':'3', 'Key':'val2'},
    {'Time':'8', 'Key':'val2'}
]

Using a for loop with any

res = []
for i in lst:
    if not any(i['Key'] in x.values() for x in res):
        res.append(i)
    else:
        pass

print(res)
# [{'Time': '1', 'Key': 'val1'}, {'Time': '3', 'Key': 'val2'}]

Can be done with itertools.groupby

res = []
for _, g in groupby(lst, key = lambda x: x['Key']):
    res.append(list(g)[0])

itertools.groupby with list comprehension and operator.itemgetter

from itertools import groupby
from operator import itemgetter

res = [list(g)[0] for _, g in groupby(lst, key=itemgetter('Key'))]
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

You can also collect items in a dictionary as you reduce and only add if the key isn't present:

from functools import reduce

lst =  [
    {'Time': '1', 'Key':'val1'},
    {'Time':'2', 'Key':'val1'},
    {'Time':'3', 'Key':'val1'},
    {'Time':'3', 'Key':'val2'},
    {'Time':'8', 'Key':'val2'}
]

def collect_if_absent(acc, d):
    if d['Key'] not in acc:
        acc[d['Key']] = d
    return acc

res = list(reduce(collect_if_absent, lst, {}).values())
print(res)
# [{'Time': '1', 'Key': 'val1'}, {'Time': '3', 'Key': 'val2'}]

This should take O(n).

slider
  • 12,810
  • 1
  • 26
  • 42