0

I have a variable like this:

self.playlist = {
                    "property_1":value_1,
                    "property_2":value_2,
                    ...
                    "items":[]
}

where items array will contain objects of type self.playlist.

How can I insert data in self.playlist object? Let's have as input the playlist_items_sequence which i want to put data, the property_n and the value_n.

If playlist_items_sequence = [0,2,7] (zero will always be the first index, something like root) i want to put data to self.playlist["items"][2]["items"][7]["propery_n"] = value_n.

Another example: if playlist_items_sequence = [0,1,4,0] I want to execute self.playlist["items][1]["items"][4]["items"][0]["property_n"] = value_n

The simplest solution I thought is to use eval() and exec() for this data structure. But I am sure there will be a more clever solution.

Edit

If I do something like:

sub_playlist = self.playlist
for index_number in playlist_items_sequence[1:]:
     sub_playlist = sub_playlist["items"][index_number]
sub_playlist["property_n"] = value_n

the self.playlist variable will not be updated.

But if I do:

exec_str = "self.playlist"
for index_number in playlist_items_sequence[1:]:
     exec_str += "[\"items\"]["+str(index_number)+"]"
exec_str += "[\"property_n\"] = \""+str(value_n)"\""
exec(exec_str)

self.playlist variable will be updated.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chris P
  • 2,059
  • 4
  • 34
  • 68
  • So does it just alternate between the key `"items"` and everything else? Like `[a, b, c, d, e] -> playlist["items"][a]["items"][b]["items"][c]["items"][d]["items"][e]["property_n"]`? – Eric Jin Jun 10 '22 at 21:15
  • @EricJin i did not understand your question. – Chris P Jun 10 '22 at 21:17
  • Wait I got it wrong, so the first number is 0, and then for the other ones you do `playlist["items"][secondnumber]["items"][thirdnumber]` and then `["property_n] = value_n`? – Eric Jin Jun 10 '22 at 21:19
  • Regarding your edit to the question: I can't reproduce your first code sample there. Recursively going into the dictionary does change the original value (the variables point to lists, not immutable types). Which version of python is this for? – Eric Jin Jun 10 '22 at 21:40
  • Yes i test it, with python 3.10 in ubuntu 22.04 and you are write! So there is no problem with the first approach! Is this a standar?: In for loop i call a sub item and i change it's value, then the value also be changed to the first main variable. (call by reference). ??? – Chris P Jun 10 '22 at 21:45
  • The variables are pointers to the actual list objects. If you've ever tried the old trick of `a = []` then `b = a` and figuring out `b` changes when `a` changes, it's that. See this question: https://stackoverflow.com/questions/10262920/understanding-pythons-call-by-object-style-of-passing-function-arguments. In fact, accessing members of a list actually calls `__getitem__` and `__setitem__` under the hood, which modifies the object itself. – Eric Jin Jun 10 '22 at 21:48
  • Ok, but now trying to delete for example: `del sub_playlist` doesn't work. – Chris P Jun 11 '22 at 00:19

0 Answers0