-2

What I mean by "recursive" is something like this: [["hello", world, [1, 2, 3]], ["foo"]]. How would I append 4 to the [1, 2, 3] list?

I have the class:

class RabbitHole:
    def __init__(self, new_name, new_holes):
        self.name = new_name
        self.holes = new_holes

(holes is a list of RabbitHole objects.)

I want to append to the hole hat that has the "path" (names of parent holes) clothes/head/hat. How would I change/append something to the hat hole while preserving the whole "directory tree" (I've done something like this in go but I can't figure out how to do it in python.)

Geremachek2
  • 121
  • 1
  • 6
  • How about making your class extend `dict` so you could do `clothes["head"]["hat"]["new_name"] = new_hole`? – Stuart Feb 11 '20 at 04:56
  • how would I wrap this in a function that takes the format "clothes/head/hat", I don't think it's possible using this method – Geremachek2 Feb 11 '20 at 05:05
  • These solutions should help with that step: https://stackoverflow.com/questions/31033549/nested-dictionary-value-from-key-path https://stackoverflow.com/questions/47969721/get-dictionary-key-by-path-string/47969823#47969823 – Stuart Feb 11 '20 at 05:23

1 Answers1

2

Assuming you have a multi-dimensional list per your example, you can do:

my_list = [['hello', 'world', [1, 2, 3]], ['foo']]

my_list[0][2].append(4)

print(my_list)

Which yields:

[['hello', 'world', [1, 2, 3, 4]], ['foo']]
Daniel
  • 3,228
  • 1
  • 7
  • 23