I don't understand this code


Let’s say you have:

node = self.graph_dict[current_room]

what does putting ‘current room’ in ‘self.graph_dict’ return?

this is for problem 22 in the link


Hey @jaccobtw.

I can’t see the exercise you’re working on as I’m not Pro, but…

Two common data structures in Python are lists and dictionaries.

A list is as straightforward as the name suggests, it’s a list of objects that you can reference by their position in the list. Their position is called the index.

So, if we instantiate a variable called “my_stuff” and assign a list to it…

my_stuff = ['socks','t-shirts','coffee','vuvuzela']

…I can retrieve a single item from the list by referencing the variable containing the list, and providing the appropriate index. For example, my_stuff[2] is 'coffee'.

A dictionary is a little bit different, as it contains key - value pairs. All this means is that instead of each child object having a numerical index based on its position in the list, they have a key index that you define. We can change the my_stuff variable to a dictionary, like so:

my_stuff = {
  'clothes' : ['socks','t-shirts'],
  'groceries' : ['coffee'],
  'random' : ['vuvuzela']

Now, if I wanted to see what clothes I had, I can just get that information using the appropriate key - my_stuff['clothes'] returns ['socks','t-shirts'].

As your code is node = self.graph_dict[current_room], it seems a safe bet to presume that graph_dict is a dictionary object. current_room therefore is the key being looked for in the dictionary, and so node will be the value from the corresponding key.

Does that help at all? :slight_smile: