[Solved] Creating a unique dictionary key


the timestamp serves as an unique id so i can give id to the rows of the cart if they need to be deleted

You’re barking up the wrong tree, trying to fix a problem in a solution not fit for the original problem; i.e. you have an XY problem.

The simplest solution: use a list instead of a dict. The list will have unique keys by definition. The only issue with a list is that in a web scenario, you may be handling two parallel or outdated requests, and the index may refer to a different element by the time it reaches your server.

So to use truly unique, unchanging ids, use the uuid module:

from uuid import uuid4

a = [1 ,2 ,3 , 4 , 5]
b = {}
for i in a:
    b[uuid4()] = i

There you go, guaranteed unique ids.

solved Creating a unique dictionary key