0

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?

cde
  • 317
  • 1
  • 18
  • use "copy" to create shallow or deep copies. – Muku Dec 29 '17 at 07:02
  • @MukulSharma that doesn't help me too much. I could manually create the copies, it's being able to address them with a variable that I'm looking for. – cde Dec 29 '17 at 07:03
  • Have you tried with the accessing them lightstate["xy"][0], it will work – Muku Dec 29 '17 at 07:10
  • Yes, that works. It gives the first value of "xy". Now I want to make a multidimensional array of this dictionary, so I can access it like I would in C. So lightstate[1]["xy"], lightstate[2]["xy], and so on, where 1 and 2 are values of the variable "count" – cde Dec 29 '17 at 07:13
  • You mean you want a list of these dictionaries ? I changed my code in this way. – vvvvv Dec 29 '17 at 07:14
  • @Vinzee is thats what I need, then yes :) – cde Dec 29 '17 at 07:14

1 Answers1

2

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"]  
vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • So how would I get the value? newdicts[0]["on"] ? What about for "xy" which is a list? newdicts[0]["xy"][0]? – cde Dec 29 '17 at 07:02
  • @Vinzee shallow or deep copies can be created with "copy". I think dict(lightstate) might be slower since in case of list(something) we iterator over the list to return a list_object – Muku Dec 29 '17 at 07:03
  • @MukulSharma I thought `copy` was making a shallow copy but not a deep copy. But I may be mistaking – vvvvv Dec 29 '17 at 07:05
  • @Vinzee copy.copy() returns a shallow copy and copy.deep_copy() returns a deep copy – Muku Dec 29 '17 at 07:07
  • @MukulSharma You mean `copy.deepcopy()` without the underscore ? I didn't know about it – vvvvv Dec 29 '17 at 07:09
  • @Vinzee yes, i mistakenly put underscore there – Muku Dec 29 '17 at 07:11
  • 1
    @Vinzee https://stackoverflow.com/questions/5861498/fast-way-to-copy-dictionary-in-python looks like its pretty much the same – Muku Dec 29 '17 at 07:12
  • That seems to be what I need. But the last thing is, can the new_dicts[0]["xy"] still be accessed as a list. As in x value = new_dicts[0]["xy"][0] (part of the code accesses lightshow["xy"][0] for the first value of that list. – cde Dec 29 '17 at 07:17
  • 2
    Yes, sure ! `x_value = new_dicts[0]["xy"][0]` and `y_value = new_dicts[0]["xy"][1]` – vvvvv Dec 29 '17 at 07:18