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.