Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add keys to a dictionary with automatically incremented values

Tags:

python

I have a dictionary, d, with values 0,1,2 for the 3 keys:

{'-11111': 0, 'hello kitty': 1, 'hello this is me': 2}

I want to add 2 new keys from a list and automatically increment their values as 3 and 4

I can get this far

newkeys = ['give a dog a bone', 'take a dog for a walk']
d.update([newkeys])

gives an updated dictionary, but with no values assigned to the new keys

{'-11111': 0,
 'hello kitty': 1,
 'hello this is me': 2,
 'give a dog a bone': 'take a dog for a walk'}

what I want is this:

{'-11111': 0,
 'hello kitty': 1,
 'hello this is me': 2,
 'give a dog a bone': 3,
 'take a dog for a walk': 4}

Is there an efficient and simple way to do this? thanks

like image 666
Michael K Avatar asked Oct 08 '19 22:10

Michael K


People also ask

How do you increment a key in a dictionary?

get() to increment a value in a dictionary. Use dict. get(key, 0) to get the current value of key in dict , if key is present in dict , and otherwise return 0 . Then, assign dict[key] to 1 plus the result of dict.

Can we create a dictionary with repeated keys?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.

Can we repeat the values of Key in dictionary?

Keys in a dictionary can only be used once. If it is used more than once, as you saw earlier, it'll simply replace the value.


1 Answers

Note: this will only work for Python 3.8+. Before 3.8, the dict type was not insertion-ordered, and therefore any attempt to do what OP wants would be much more of a pain in versions < 3.8.

A simple solution

Here's a simple solution which doesn't require importing any modules:

{val: idx for idx, val in enumerate([*old_dict, *new_keys])}

A quick demo:

>>> old_dict = {'-11111': 0, 'hello kitty': 1, 'hello this is me': 2}
>>> new_keys = ['give a dog a bone', 'take a dog for a walk']
>>> new_dict = {val: idx for idx, val in enumerate([*old_dict, *new_keys])}
>>> new_dict
{'-11111': 0,
 'hello kitty': 1,
 'hello this is me': 2,
 'give a dog a bone': 3,
 'take a dog for a walk': 4}

This requires building a new dictionary, and in addition to other solutions will not account for duplicate keys (e.g., if you add another "-11111" to new_keys, the old one will be erased, and your count won't be updated).

A nice bonus of using enumerate() is that it allows for starting your counter at any value by using the optional start parameter:

>>> {val: idx for idx, val in enumerate([*old_dict, *new_keys], -99)}
{'-11111': -99,
 'hello kitty': -98,
 'hello this is me': -97,
 'give a dog a bone': -96,
 'take a dog for a walk': -95}

Alternative solution with optional start and step size

If you want a custom starting value and a custom step size:

def new_dict(old_dict, new_keys, start=0, step=1):
    keys = [*old_dict, *new_keys]
    stop = len(keys) * step
    return dict(zip(keys, range(start, stop, step)))

Demo:

>>> new_dict(old_dict, new_keys, step=2)
{'-11111': 0,
 'hello kitty': 2,
 'hello this is me': 4,
 'give a dog a bone': 6,
 'take a dog for a walk': 8}

>>> new_dict(old_dict, new_keys, step=3)
{'-11111': 0,
 'hello kitty': 3,
 'hello this is me': 6,
 'give a dog a bone': 9,
 'take a dog for a walk': 12}

>>> new_dict(old_dict, new_keys, start=-4, step=3)
{'-11111': -4,
 'hello kitty': -1,
 'hello this is me': 2,
 'give a dog a bone': 5,
 'take a dog for a walk': 8}
like image 50
ddejohn Avatar answered Sep 29 '22 21:09

ddejohn