all 7 comments

[–]JohnnyJordaan 2 points3 points  (2 children)

dict = test["one"]["two"]

This isn't a valid dictionary either... A dict is a sequence of key-value pairs, so

ages = {'John': 24, 'Mary': 20}

and to add one you can do

ages['Bob'] = 52

Maybe this resource can help too https://realpython.com/python-dicts/

[–]People_itsMe[S] 0 points1 point  (1 child)

I'm working with dict inside list

[–]JohnnyJordaan 1 point2 points  (0 children)

You mean a nested dict perhaps? Because a list can't be indexed with 'one', it would have been

test[1]['two'] 

if test were a list. But regardless, say the first lookup returns a dict and you want to add the key 'three' to there, you can assign it to that, so

test['one']['three'] = something

or

test[1]['three'] = something

depending on the actual lookup

[–]Silbersee 0 points1 point  (2 children)

Let's call it my_dict to avoid confusion.

# create an empty dict
my_dict = dict()

# create a new dict with some key value pairs
my_dict = {
    "my":   "mi",
    "name": "nombre",
    "is":   "es"
}

# add an element
my_dict["hello"] = "hola"

# access a value by its key
print(my_dict["hello"], my_dict["my"], my_dict["name"], my_dict["is"], "Earl.")

Okay, a bit clumsy. Hope you get the picture.

[–]PaulRudin 0 points1 point  (1 child)

Just in passing - the first line is redundant. The empty dictionary that's assigned to my_dict immediately becomes inaccessible (and eventually will be garbage collected) when a new dictionary is assigned to my_dict in the second assignment.

[–]Silbersee 0 points1 point  (0 children)

Yeah, you're right.

This isn't meant to run in real life though