Changing a key value in a Python dictionary

Asked

Viewed 279 times

1

I need to get the position of one of the key values 'Codigo', for example 321, and in the same position change the key value 'Status aluguel' for 'S'. How can I do that?

Imoveis_Cadastrados = {
  'Codigo': [123, 321],
  'CPF_PROP': [],
  'Tipo': [],
  'Endereço': [],
  'Valor_Aluguel': [],
  'Status_Aluguel': ['N', 'N']
}

The point is to keep it that way:

Imoveis_Cadastrados = {
  'Codigo': [123, 321],
  'CPF_PROP': [],
  'Tipo': [],
  'Endereço': [],
  'Valor_Aluguel': [],
  'Status_Aluguel': ['N', 'S']
}

2 answers

3

You don’t need the key to the 'Código' to access key values 'Status_Aluguel'. You use the keys to access the values directly. For example:

Imoveis_Cadastrados['Status_Aluguel'] = ['N', 'S']

Now let’s assume that you have a large list inside each key of the dictionary. In this case, an option is to use the method index() to find the position corresponding to the desired value (in this case, 321) on the list of 'Código'. Then you use this same value as input to change the same position in the list of 'Status_Aluguel':

indice = Imoveis_Cadastrados['Codigo'].index(321)
Imoveis_Cadastrados['Status_Aluguel'][indice] = 'S'

2

Your dictionary is "backwards".

The correct is not to use a dictionary so that the values are lists and each position in the lists, in all values in the dictionary, corresponds to a record (that is: the set of information relating to an object).

The correct is to have a list, where each element in the list is a dictionary, and then all the information in your record comes together.

In case, you would have something like:

imoveis_cadastrados = [
{'Codigo': 123 'CPF_PROP': None, 'Tipo': None, 'Endereço': None, 'Valor_Aluguel': None, 'Status_Aluguel': 'N'},
 {'Codigo': 321, 'CPF_PROP': None, 'Tipo': None, 'Endereço': None, 'Valor_Aluguel': None, 'Status_Aluguel': 'N'}
]

there, ready.

You can do a linear search doing something like this:

imovel = [im for im in imoveis_cadastrados if im["código"] == 321]
imovel["Status_Aluguel"] = "S"

If you always want to locate by code, you can use the outside structure as a dictionary, where the key is always the code - there you don’t need the "Code" key in the inside dictionaries:

imoveis_cadastrados = {123: {'CPF_PROP': None, 'Tipo': None, 'Endereço': None, 'Valor_Aluguel': None, 'Status_Aluguel': 'N'},
 321: {'CPF_PROP': None, 'Tipo': None, 'Endereço': None, 'Valor_Aluguel': None, 'Status_Aluguel': 'N'}}

And to change the status of a property, given the code:

imoveis_cadastrados[321]["Status_Aluguel"] = "S"

If you need to locate furniture by different code criteria, such as "list all properties of CPF xxxx", then it is best to use an SQL structure instead of dictionaries.

Python comes with the built-in sqlite database, which can store the data in memory, which would have the equivalent behavior of a dictionary, but can also store the values in a file, which will be kept synchronized. If your problem is part of a real program you are doing, saving your data in a file is something you will want to do anyway.

And the advantage of using SQL in this case is less being any kind of efficiency cariterio - unless you’re going to have a registry with tens of thousands of properties, and yes, because the SQL language allows you to express these searches in a natural way. (Although the recommendation for real projects is, take another step and use a "wrapper" for SQL, and use everything as Python classes, with the help of an ORM like Sqlalchemy or the Django framework)

  • 1

    another tip, unrelated to your problem - a matter of style, but one that is very practical: do not use names by mixed uppercase and lowercase And underscores - will only give you trouble to type, and are difficult to read - everything in lowercase is more enjoyable to read - and, when displaying in the user interface, there are functions like the method .title of strings that can turn initials into uppercase if you find it interesting.

Browser other questions tagged

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