Assuming a list of dictionaries
l=[
{"id":1, "score":80, "remarks":"B" },
{"id":2, "score":80, "remarks":"A" },
{"id":1, "score":80, "remarks":"C" },
{"id":3, "score":80, "remarks":"B" },
{"id":1, "score":80, "remarks":"F" },
]
I would like to find the indexes of the first unique value given a key. So given the list above i am expecting a result of
using_id = [0,1,3]
using_score = [0]
using_remarks = [0,1,2,4]
What makes it hard form me is the list has type of dictionary, if it were numbers i could just use this line
indexes = [l.index(x) for x in sorted(set(l))]
using set() on a list of dictionary throws an error TypeError: unhashable type: 'dict'
.
The constraints are: Must only use modules that came default with python3.10,the code should be scalable friendly as the length of the list will reach into the hundreds, as few lines as possible is a bonus too :)
Of course there is the brute force method, but can this be made to be more efficient, or use lesser lines of code ?
unique_items = []
unique_index = []
for index, item in enumerate(l, start=0):
if item["remarks"] not in unique_items:
unique_items.append(item["remarks"])
unique_index.append(index)
print(unique_items)
print(unique_index)