Reputation: 327
I have little experience in Python, more in C. I am used to arrays of arrays, and that would be simple for me to do what I want, but this is not.
So I have a dictionary, which also has a list inside:
lightstate = {"on" : False, "bri": 0, "xy": [0.0, 0.0], "ct": 153}
I want to make a few copies of it, which I can address with the value of a variable.
In C, it would be arrayname[varname][0], so I would get lightstate 1, "on". Here, in python, I am not sure. The "xy" list also throws me off. To note, I am modifying existing code, so I can't just scrap it all.
So how can I do this?
Upvotes: 0
Views: 79
Reputation: 31670
In Python, you can copy a dict
object like this:
new_dict = dict(lightstate)
So if you want to make a few copies of it and store them in a list
for example, you can write:
nb_copies = 10 # Your number of copies
new_dicts = [dict(lightstate) for i in range(nb_copies)]
You can access the values of the dictionaries by doing so:
# lightstate = {"on" : False, "bri": 0, "xy": [0.0, 0.0], "ct": 153}
on_value = new_dicts[0]["on"]
bri_value = new_dicts[0]["bri"]
xy_value = new_dicts[0]["xy"] # It is a list: [0.0, 0.0]
ct_value = new_dicts[0]["ct"]
Upvotes: 2