Find if a key exists in python repl db

Question:
I want to get a value from the repl db and increment it. Problem is, there isnt a way (that i can see) to find if a key actually is in the database, so i can set it to 0 and then increment it. How would i do this?

I think this code should work, though I have not tested it. You might need to make the value a string because I don’t think numbers can go in ReplitDB.

from replit import db

value = db.get("key", 0)

dict.get() basically just returns the value of a key if it exists. If it doesn’t exist it returns the second parameter, or None if the second parameter isn’t set.

Please mark this post as the solution if it solved your problem so the thread can close.

5 Likes

I tried, and it worked thanks! Btw numbers can go in replit db, i didnt need to change anything

2 Likes

You don’t even need the 0, the python standard for explicit get is to return None if not found.

They want it to be 0…

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.