Key-value dictionary

Asked

Viewed 73 times

1

Suppose I have the following dictionary:

dicionario = {'exemplo1':1, 'exemplo2':2, 'exemplo3':3}

How do I add a key-value without changing the ones already added? In case I want it to stay that way:

#Como eu quero
dicionario = {'exemplo1':1, 'exemplo2':2, 'exemplo3':3, 'exemplo1':100}

I’ve tried it a few ways here, but I can only get it to update the value of the key that has already been added.. That’s how it works.:

#Como fica:
dicionario = {'exemplo1':100, 'exemplo2':2, 'exemplo3':3}
  • Adriano, it is important you know that every key inside a dictionary is unique... IE, you can not have two keys exemplo1... If you assign a value to an existing key you will overwrite it.

3 answers

2


First of all, one feature of the dictionary is to have A value for each different key. If you want two values, you need to use another structure (or use a structure instead of the simple value). When you repeat a key, you are overwriting the previous value.

About adding, that’s basically it:

dicionario['novachave'] = 'novovalor'

Applying to your case and testing:

dicionario = {'exemplo1':1, 'exemplo2':2, 'exemplo3':3}
print(dicionario)
dicionario['exemplo4'] = 100
print(dicionario)

IMPORTANT: note that I changed the key name to avoid overwriting

There are other ways, such as the method update mentioned in Lucas' reply. The update is more interesting if you want to add more values:

my_dict.update({'exemplo4': 100, 'exemplo5': 500})


See the two ways working on IDEONE.

Handbook:

https://docs.python.org/pt-br/3.8/tutorial/datastructures.html#Dictionaries

  • Show! I had read about it but I needed to confirm it. Thanks anyway.

1

You cannot have two keys with the same name. If you want to add cases with keys different you can use the method update:

my_dict = {'exemplo1':1, 'exemplo2':2, 'exemplo3':3}
my_dict.update({'exemplo4': 4})
  • What is the use speed dict.update() to assign only one key over dict['chave'] = valor? This would not affect the performance of your code?

  • 1

    Update has nothing to do with performance - it has to do with, whether the keys and values you are going to update are as data (you have received a dictionary with new data in a function, or you have read the valve keys you are going to update from a file), against - already know, when writing a function, which keys it will update - hence the name of the key can be directly in your file . py, as part of the program. In terms of performance, the difference is negligible anywhere.

0

As mentioned, a key is unique, but nothing prevents you from having a list as a value, so you can store multiple elements per key. For consistency and simplicity, I would use a defaultdict list:

from collections import defaultdict

my_dict = defaultdict(list)
my_dict['exemplo1'].append(1)
my_dict['exemplo1'].append(100)
my_dict['exemplo4'].append(1337)

print(my_dict)
# defaultdict(<class 'list'>, {'exemplo1': [1, 100], 'exemplo4': [1337]})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.