Add keys to a dictionary with automatically incremented values

python dictionary increment key
python dictionary increment value if key exists
python increment value by 1
how to add keys and values to a dictionary in python using for loop
python sort dictionary by value
add one to value in dictionary
how to add another value to a key in dictionary python
add 1 to dict value 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


You could iterate over the list of newkeys and set each new key to the length of list of items currently in the dict:

d = {'-11111': 0, 'hello kitty': 1, 'hello this is me': 2}
newkeys = ['give a dog a bone', 'take a dog for a walk']
    newkeys = ['give a dog a bone', 'take a dog for a walk']
    for key in newkeys:
        d[key] = len(d.items())

To add new keys, simply use d[keyname] = keyvalue, and the key is appended.

Python, It may seem a quite straight forward problem, but catch comes when the existence of a key is not known, hence becomes a 2 step process at times. Let's discuss  d = {'key': 'value'} print(d) # {'key': 'value'} d['mynewkey'] = 'mynewvalue' print(d) # {'key': 'value', 'mynewkey': 'mynewvalue'} you create a new key\value pair on a dictionary by assigning a value to that key. If the key doesn't exist, it's added and points to that value. If it exists, the current value it points to is overwritten. —R. Navega


One other option assuming you don't want to start from 0 (but want to increment)

Class CustomIncrementer(object):
    def __init__(self, initial_value_or_dictionary):
        if isinstance(initial_value_or_dictionary, dict):
            # Assumes that the values in the input dictionary are only numerics
            self.my_dict = initial_value_or_dictionary
            self.counter = max(initial_value_or_dictionary.values()) + 1
        else:  # assumes it's a numeric 
            self.my_dict = {}
            self.counter = initial_value_or_dictionary

    def update(self, list_of_words: list):
        for element in list_of_words:
            self.my_dict[element] = self.counter
            self.counter += 1

How to increment a value in a dictionary in Python, 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. Create a dictionary first. This method will create a new key\value pair on a dictionary by assigning a value to that key. If the key doesn’t exist, it will be added and will point to that value. If the key exists, the current value it points to will be overwritten.


Considering you know exactly what the keys and values should be, why don't you just do:

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

In each sublist, the first item becomes a key and the second one the associated value.

EDIT:

The same idea, with the incremental feature

d = {'-11111': 0, 'hello kitty': 1, 'hello this is me': 2}
newkeys = ['give a dog a bone', 'take a dog for a walk']
d.update([[x, y + len(d)] for y, x in enumerate(newkeys)])
# {'-11111': 0, 'hello kitty': 1, 'hello this is me': 2,
#  'give a dog a bone': 3, 'take a dog for a walk': 4}

How to Sort a Dictionary by Value in Python, How do you sort a dictionary by value in Python? Python | Combine two dictionary adding values for common keys Given two dictionary, the task is to combine the dictionaries such that we get the added values for common keys in resultant dictionary. Example:


Restrictions on Dictionary Keys and Values – Real Python, () takes three arguments: object, key, and reverse. Dictionaries are unordered data structures. Python dictionary increment. Ask Question but incrementing two values per key not one – Mohsin Jun 5 '17 at 4:19. 1. How can I add new keys to a dictionary?


Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5, How do you check if a key is in a dictionary python? If you access the defaultdict using a key, and the key is not already in the defaultdict, the key is automatically added with a default value. The defaultdict takes the callable you passed in, and calls it to get the default value. In this case, we passed in class int; when Python calls int () it returns a zero value.


Dictionaries, Which of the following types are allowed for python dictionary keys? dict.update (Iterable_Sequence of key:value) It accepts in iterable sequence of key value pairs that can be single key value pair or list of tuples or an another dictionary. For each entry in sequence, it will add the given key value pair in dictionary and if key already exists then it will update its value. Let’s use this to add new key value pairs in dictionary or updating existing one.