Feb-20-2019, 11:48 PM
Suppose I have a dictionary named "properties".
properties = {}
And there's a few different items that will be subkeys.
properites["item1"] = {}
One of the subkeys is "color".
properites["item1"]["color"] = "blue"
What's the most efficient way to define item2's color if item 2 doesn't exist in the properties dict yet? So:
properties = {}
properties["item2"]["color"] = "black"
# throws a KeyError: 'item2'
Currently I'm solving this with a roundabout declaration:
properites = {}
properties["item2"] = {}
properties["item2"]["color"] = "black"
But I'm guessing there's a better way.
properties = {}
And there's a few different items that will be subkeys.
properites["item1"] = {}
One of the subkeys is "color".
properites["item1"]["color"] = "blue"
What's the most efficient way to define item2's color if item 2 doesn't exist in the properties dict yet? So:
properties = {}
properties["item2"]["color"] = "black"
# throws a KeyError: 'item2'
Currently I'm solving this with a roundabout declaration:
properites = {}
properties["item2"] = {}
properties["item2"]["color"] = "black"
But I'm guessing there's a better way.