Home > Blockchain >  Does update allow integers as keyword arguments?
Does update allow integers as keyword arguments?

Time:11-24

I know I can update a dictionary using other means. I'm curios about this specific way of updating a dictionary.

Update works (see below)

dict1 = {1: 'cat', 2: 'dog'}
dict1.update(k3 = 'blabla', k4 = 'blabla')
print(dict1)

Update does not work (see below)

dict1 = {1: 'cat', 2: 'dog'}
dict1.update(3 = 'blabla', 4 = 'blabla')
print(dict1)

Can I update a dictionary using key value pairs in this way when the key is an integer?

CodePudding user response:

Not exactly like this. Keyword arguments must be strings. Interestingly enough, you are able to include keyword arguments that aren't legal identifiers; for example, you can do dict1.update(**{"-": 1}), but you cannot do dict1.update(**{1: 2}).

You can do dict1.update({1: 2, 3: 4}) though. Try it online!

  • Related