I have code similar to this. Thing is, I have a dictionary containing key, value pairs obtained from some data processing. BUT sometimes the obtained key is empty, and a placeholder has to be created for each value with an empty key, consisting on a prefix and a numeric suffix.
No, I can't use an array because all the data has to be stored on the dictionary, and 99% of the time there will be proper keys, it's just that sometimes a key cannot be retrieved and since it cannot be empty a name has to be generated.
index = 0
keyname_pattern = '[prefix_{0:02d}]'
while keyname_pattern.format(index) in some_dictionary:
index += 1
self.current_key = keyname.format(index)
# They value is stored in the dictionary at other point in the code.
Thing is, this works perfectly, but for me looks… unpythonic, I can't explain why.
Can this be written in a more compact, explicit, pythonic or just better way?
Thanks a lot!