1

Edit: How would i specificly create a nested dictionary in folder["one.1"][1], meaning as second item in its list?

im trying to write a program that can create dictionaries in dictionaries with infinite deepness, but right now I only get one deep. I mean i get maximum {"one": {"one.1": "blabla"} but I want to make it possible to get infinitely deep so that I for example can create {"one": {"one.1": {"one.2": {"one.3": "blabla"}}}}.

This is my code right now:

folder = {"one": {"one.1": ["blabla"]}}
def add_folder(curr_folder):
    """
    adds a folder name and context to the mainfolder
    """
    name = input("Folder name: ")

    # control to ensure that no file gets overwritten
    if name in curr_folder:
        print("this name is already used")
        return

    context = input("{} context: ".format(name))
    curr_folder[name] = context
    print(curr_folder)

    return

def add_nested_folder(curr_folder):
    """
    adds a nested folder to the specified folder.
    """
    name = input("Name of deep folder: ")

    # control to ensure that no file gets overwritten
    if name in curr_folder:
        print("this name is already used")
        return

    context = {name: input("context of deep folder: ")}
    curr_folder[name] = context
    print(curr_folder)
    return

add_nested_folder(folder)

Im very thankful for any suggestions. Cheers

mcklmo
  • 11
  • 3
  • Not sure how you are calling the functions, but if you want to nest something on top of the nested folder, you generally want to recursively run through add_nested_folder. – Andrew Holmgren Apr 26 '21 at 16:14
  • @AndrewHolmgren thanks for your answer. I edited the post to make the question clearer. What do you say? – mcklmo Apr 27 '21 at 05:09
  • Does this answer your question? [What is the best way to implement nested dictionaries?](https://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries) – bad_coder Apr 27 '21 at 10:47
  • I'm wondering if you'd rather use a Trie, which could be represented as nested dicts but may be cleaner. https://en.wikipedia.org/wiki/Trie#:~:text=In%20computer%20science%2C%20a%20trie,key%2C%20but%20by%20individual%20characters. – Andrew Holmgren Apr 28 '21 at 16:24

0 Answers0