'How to add to dictionary if the value exists in python? [duplicate]
I'm creating a dictionary from json data. I have a value 'actions' which is not on all the descriptions but I would like to add it to the ones that have it. How would I do it?
I tried to do it that way item['actions'] if item['actions'] else None but I'm still getting a keyerror with it.
desc = {item['classid']:
[
item['name'],
item['market_hash_name'],
item['icon_url'],
item['tradable'],
item['actions'] if item['actions'] else None
] for item in inventory['descriptions']}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
