Roy Nvn
Roy Nvn

Reputation: 15

unhashable type: 'dict'

I am new in here and want to ask something about removing duplicate data enter, right now I'm still doing my project about face recognition and stuck in remove duplicate data enter that I send to google sheets, this is the code that I use:

if(confidence <100):
    id = names[id]
    confidence = "{0}%".format (round(100-confidence))
    row = (id,datetime.datetime,now().strftime('%Y-%m-%d %H:%M:%S'))
    index = 2
    sheet.insert_row (row,index)
    data = sheet.get_all_records()
    result = list(set(data))
    print (result)

The message error "unhashable type: 'dict" I want to post the result in google sheet only once enter

Upvotes: 0

Views: 1206

Answers (2)

alberand
alberand

Reputation: 662

According to documentation of gspread get_all_records() returns list of dicts where dict has head row as key and value as cell value. So, you need to iterate through this list compare your ids to find and remove repeating items. Sample code:

visited = []
filtered = []
for row in data:
    if row['id'] not in visited:
        visited.append(row['id'])
    else:
        filtered.append(row)

Now, filtered should contain unique items. But instead of id you should put the name of the column which contains repeating value.

Upvotes: 0

JoshG
JoshG

Reputation: 6745

You can't add dictionaries to sets.

What you can do is add the dictionary items to the set. You can cast this to a list of tuples like so:

s = set(tuple(data.items()))

If you need to convert this back to a dictionary after, you can do:

for t in s:
    new_dict = dict(t)

Upvotes: 2

Related Questions