Home > Software engineering >  How do you use a variable to reference one of multiple dictionaries so that you can edit that one di
How do you use a variable to reference one of multiple dictionaries so that you can edit that one di

Time:03-29

I have three dictionaries being used in my program and need to be able to add or remove an item to any of the three dicts. How do I tell the program which dict to add the item to through the use of a variable? I can't simply put "DictName[NewKey] = NewValue" because it depends on which dict the user has chosen.

Example:

Dict1 = {"Key1": "Value1"}
Dict2 = {"Key2": "Value2"}
Dict3 = {"Key3": "Value3"}

var = input("Please choose a dictionary") #Dict1, Dict2, or Dict3

print("Add item to", var)
var[Key4] = Value 4

My issue is that I cant use the variable to call on the dict and change edit it.

CodePudding user response:

Use a dict of dicts:

super_dict = {
    "Dict1": {"Key1": "Value1"},
    "Dict2": {"Key2": "Value2"},
    "Dict3": {"Key3": "Value3"}
}

Then you can access them that way:

var = input(f"Please choose a dictionary: {', '.join(super_dict.keys())}") #Dict1, Dict2, or Dict3

print("Add item to", var)
super_dict[var]["Key4"] = "Value 4"

CodePudding user response:

How about a dictionary of dictionaries? If the user enters the string "Dict1", for example, you can de-reference that particular dictionary this way:

Dict1 = {"Key1": "Value1"}
Dict2 = {"Key2": "Value2"}
Dict3 = {"Key3": "Value3"}

dict_of_dicts = {
    "Dict1": Dict1,
    "Dict2": Dict2,
    "Dict3": Dict3,
}

print(dict_of_dicts["Dict1"]["Key1"])
Value1

CodePudding user response:

Manage them by a new dict

Dict1 = {"Key1": "Value1"}
Dict2 = {"Key2": "Value2"}
Dict3 = {"Key3": "Value3"}

dicts = {
    "1": Dict1,
    "2": Dict2,
    "3": Dict3,
}
var = input("Please choose a dictionary") # 1 or 2 or 3

print("Add item to", var)

dicts[var][key4] = Value 4

CodePudding user response:

Using a new dictionary of dictionaries like the previous answers works well, but if there are a constant amount of dictionaries you can use a simple if-elif-else statement. Of course, if there are varying numbers of dictionaries then it is better to use a dictionary of dictionaries. An if-elif-else statement example follows:

Dict1 = {"Key1": "Value1"}
Dict2 = {"Key2": "Value2"}
Dict3 = {"Key3": "Value3"}

var = input("Please choose a dictionary") #Dict1, Dict2, or Dict3

print("Add item to", var)
if var == "Dict1":
    # do what you want to Dict 1 here
elif var == "Dict2":
    # do what you want to Dict 2 here
else:
    # do what you want to Dict 3 here

CodePudding user response:

This globals() method works. Its looks a bit complicated. Dictionary of Dictionary method is the cleaner one for your program. You can read more on How can you dynamically create variables?

Dict1 = {"Key1": "Value1"}
Dict2 = {"Key2": "Value2"}
Dict3 = {"Key3": "Value3"}

var = input("Please choose a dictionary: ") # 1 or 2 or 3
globals()[f"Dict{var}"] [f"Key{int(var) 1}"]= f"value{int(var) 1}"
print(globals()[f"Dict{var}"]) 
  • Related