Question 16: Find unique items from the below input.
Input:
[{"name":"test2", "role":"admin","id":2},
{"name":"test1", "role":"guest","id":1},
{"name":"test3", "role":"admin","id":3},
{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2}]
Output:
[{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2},
{"name":"test3", "role":"admin","id":3}]
Input:
[{"name":"test2", "role":"admin","id":2},
{"name":"test1", "role":"guest","id":1},
{"name":"test3", "role":"admin","id":3},
{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2}]
Output:
[{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2},
{"name":"test3", "role":"admin","id":3}]
output = []
ReplyDeletefor dit in input:
if dit not in output:
output.append(dit)
ds = sorted(output, key=lambda x: x['id'])
print(ds)