input:
input = [
{'key': '1', 'value': 'a'},
{'key': '2', 'value': 'b'},
{'key': '3', 'value': 'c'}
]
output
{
"1": "a",
"2": "b",
"3": "c"
}
What I’ve tried:
output = {list(entry.values())[0]: list(entry.values())[1] for entry in input}
print(output) #{'1': 'a', '2': 'b', '3': 'c'}
My question is there a better way of doing this instead of each entry get list of values and access to the first or second!
Any suggestion of doing it in a more simple way!
>Solution :
As entry is a dict, access the data using the keys, that is how you should manipulate a dict
values = [
{'key': '1', 'value': 'a'},
{'key': '2', 'value': 'b'},
{'key': '3', 'value': 'c'}
]
output = {entry['key']: entry['value'] for entry in values}
input is the python builtin method for reading user input, don’t use it as a variable name