Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Create a Unique Key For A Dictionary In Python

What is the best way to generate a unique key for the contents of a dictionary. My intention is to store each dictionary in a document store along with a unique id or hash so that I don't have to load the whole dictionary from the store to check if it exists already or not. Dictionaries with the same keys and values should generate the same id or hash.

I have the following code:

import hashlib

a={'name':'Danish', 'age':107}
b={'age':107, 'name':'Danish'}

print str(a)
print hashlib.sha1(str(a)).hexdigest()
print hashlib.sha1(str(b)).hexdigest()

The last two print statements generate the same string. Is this is a good implementation? or are there any pitfalls with this approach? Is there a better way to do this?

Update

Combining suggestions from the answers below, the following might be a good implementation

import hashlib

a={'name':'Danish', 'age':107}
b={'age':107, 'name':'Danish'}


def get_id_for_dict(dict):
    unique_str = ''.join(["'%s':'%s';"%(key, val) for (key, val) in sorted(dict.items())])
    return hashlib.sha1(unique_str).hexdigest()

print get_id_for_dict(a)
print get_id_for_dict(b)
like image 557
Danish Avatar asked Apr 18 '13 20:04

Danish


People also ask

Do dictionaries have unique keys Python?

A dictionary is an unordered and mutable Python container that stores mappings of unique keys to values.

Does dictionary have unique keys?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.

Can you add keys to a dictionary Python?

Python Dictionary is used to store the data in the key-value pair. You can add key to dictionary in python using mydict["newkey"] = "newValue" method. Dictionaries are changeable, ordered, and don't allow duplicate keys. However, different keys can have the same value.


1 Answers

I prefer serializing the dict as JSON and hashing that:

import hashlib
import json

a={'name':'Danish', 'age':107}
b={'age':107, 'name':'Danish'}

# Python 2
print hashlib.sha1(json.dumps(a, sort_keys=True)).hexdigest()
print hashlib.sha1(json.dumps(b, sort_keys=True)).hexdigest()

# Python 3
print(hashlib.sha1(json.dumps(a, sort_keys=True).encode()).hexdigest())
print(hashlib.sha1(json.dumps(b, sort_keys=True).encode()).hexdigest())

Returns:

71083588011445f0e65e11c80524640668d3797d
71083588011445f0e65e11c80524640668d3797d
like image 143
vamin Avatar answered Oct 07 '22 17:10

vamin