I have some problem with my function.
I do an SQL Query to obtain a list that have some dictionary with only a key/value element.
This is a sample of my output:
myDictList = [{'id': 55, 'sigla': 'SNG'}, {'id': 62, 'sigla': 'TRP'},
{'id': 71, 'sigla': 'PCNIM'}, {'id': 72, 'sigla': 'pc2ni'},
{'id': 73, 'sigla': 'ccas'}, {'id': 74, 'sigla': 'ased1'},
{'id': 75, 'sigla': '131s'}, {'id': 76, 'sigla': 'r888'},
{'id': 56, 'sigla': 'DBL'}]
The id are unique (for SQL constraint).
I’ve build a list of id in that way
r_id_list = list(rid['id'] for rid in roomList)
Now, i would build a dict. like that from myDictList:
{55:'SNG',62:'TRP',71:'PCNIM'.... and so on}
More over, i want to avoid part of code like that:
finalDict = {}
for element in myDictList:
finalDict.update({element['id']:element['sigla']})
I want to do that for some reasons:
- I don’t want to do two separate query to obtain some data that i can retrive with one
- I don’t want to iterate to every list element for retrive
siglavalue fromr_id_listelement
Some suggestion for a compact code that will not include a for cycle inside a for cycle, and increase the complexity?
EDIT1
As comment mentioned, it will be a better way to do.
EDIT2