-1

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!

  • 1
    I don't really see the problem. – Iguananaut Jun 13 '22 at 17:22
  • 2
    Does it have to be a specific prefix followed by numeric suffix? Or could it be some kind of unique ID like uuidv4? Or perhaps, assuming your autogenerated keys are guaranteed to not be in the dict, then you could use a generator function to yield them. – jarmod Jun 13 '22 at 17:22
  • Similar to https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary – Kavi Harjani Jun 13 '22 at 17:26
  • 1
    Could you clarify what you mean by _" sometimes the obtained key is empty, and a placeholder has to be created..."_, perhaps with an example? Your code looks perfectly fine to me, remember that pythonic is not synonymous with "as short as possible" or "squeezed into a single line" – Pranav Hosangadi Jun 13 '22 at 17:40
  • 1
    Whilst this may work, it could be extremely inefficient if/when there are large numbers of keys. Better to add a reserved/control key where you store the next value to be used – DarkKnight Jun 13 '22 at 17:42
  • Thanks for all the replies. Kavi, no, it's not similar, I don't have any problem checking if a key exists or not. Pranav, the key/value pairs are obtained from another piece of code I don't control (and I cannot post here). Sometimes the obtained key is empty. That's the problem. – Raúl Núñez de Arenas Coronado Jun 13 '22 at 18:06
  • jarmod, it is required, the generated key must be prefix_XX with two numbers, but the idea of a generator looks interesting. I considered it at first but looked a bit overkill for this usage. Still, is a great idea because… well, that's what generators are for! Still I prefer Albert solution. – Raúl Núñez de Arenas Coronado Jun 13 '22 at 18:10

1 Answers1

1

You could use a control value in the dictionary that contains the next value to be used when creating a "special" key.

For example:

some_dictionary = {}

CONTROL_KEY = '__CONTROL__'

def make_placeholder(dict_):
    dict_[CONTROL_KEY] = dict_.get(CONTROL_KEY, -1) + 1
    return f'[prefix_{dict_[CONTROL_KEY]:02d}]'

for _ in range(5):
    print(make_placeholder(some_dictionary))

In this way you don't have to keep searching for the next possible key. If it's likely to interfere with subsequent processing, you could always delete the control key once the dictionary is fully populated.

Output:

[prefix_00]
[prefix_01]
[prefix_02]
[prefix_03]
[prefix_04]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22