Do I need to reference locations in the dictionary with brackets?

hello, i hope i can explain myself with this, im having a lot of confusion with this:

does this part : total += my_dictionary[key]
has to go with “” because its asking for keys? in all of those cases the are necesary because in the example before with the values it didnt have, it was just as:
for value in my_dictionary.values():
total += value

thanks

Is a value with attribute name referenced by key. To access the value, key is subscripted.

are just the values in a list, which do not need to be subscripted to access them.

1 Like

why is it that we are adding the :values together?

Have you had a chance to go through the Python documentation, yet?

docs.python.org

Under Dictionaries (dict type) we find a number of useful methods, of which values() gives us back a list of values, and keys() gives back a list of keys, and items() gives back a list of key-value pair tuples.

If I understand the question, are you asking how it is that we would write,

my_dict.values()

?

The dot means it is a method of the particular class that our object is associated with. In this case, we have what is determined to be a dict object. That class of object has all the three previously mentioned methods, value, key, items. They are part of the class definition.

An object that belongs to a particular class (don’t worry, Python has it figured out which class it is) inherits all the methods and class attributes that go along with it. That’s why we can write,

my_dict.values()

when my_dict is in fact a dictionary.

I have yet to use that .doc, but have looked into.
some of the questions are kind of difficult and you really have to think about the answer.
will this be of use when programming or doing something like Machine Learning?
I think I got the answer to it. The array changes the key place to the value place.

Most definitely. We could fumble along as we try to learn what a particular function does, but reading the documentation gives us a clear idea of what to expect from the function, and how to implement it. Docs are our friend, and should be near at hand, always. Seasoned programmers lean on the documentation. That tells us we should, too.

1 Like