About dictionaries

when you used dicts in some programmes like ex47 and some places you have used [ ] i get error using that but change that to {} i get correct result.are these both same thing or just printing mistake

When creating a dict use {}

dict = {‘a’: ‘thing’, ‘b’: ‘another_thing’}

You use [] to access values within. So using the key…


…to access the value ‘thing’

What code are you changing from [] to {}? You may be changing the meaning of the code.

start.add_paths([‘west’: west, ‘down’: down])
west.add_paths([‘east’: start])
down.add_paths([‘up’: start])
in exercise 47

i get invalid syntax using [].

Oh right. That is invalid I think. In my version of the book (04/07/2017) it is:

start.add_paths({'west': west, 'down': down}) 
west.add_paths({'east': start}) 
down.add_paths({'up': start})
1 Like

Yes, you cannot use [] and make a dict. Be sure you understand this:

a_list = [1,2,3,4]
a_dict = {'age': 34, 'height': 74}

See how the list is just a sequence of numbers, but the dict is associating the strings ‘age’ and ‘height’ to numbers?