Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

introduction of dictionaries

in the introduction of dictionaries Kenneth's code in the workspace looks like this:

course = {"title": "Python Collections", "teacher": {"first_name": "Kenneth", "last_name": "Love"}
>>> course
{"teacher': {'last_name': 'Love', 'first_name': 'Kenneth'}, 'title': 'Python Collections', 'videos': 22}

I did the exact same thing and my code came out like this:

>>> course = {"title": "Python Collections", "teacher": "Kenneth Love", "videos": 22}                                                                                                    
>>> course = {"title": "Python Collections", "teacher":{"first_name": "Kenneth", "last_name": "Love"}                                                                          
... course                                                                                                                                            
  File "<stdin>", line 2      course
^                                                                                                                                                      
SyntaxError: invalid syntax  

I tried a 2nd time because I saw that it was missing a } and this was my code:

>>> course = {"title": "Python Collections", "teacher": "Kenneth Love", "videos": 22}                                                                                                    
>>> course["videos"]                                                                                                                                                                  
22                                                                                                                                                                                    
>>> course = {"title": "Python Collections", "teacher":{"first_name": "Kenneth", "last_name": "Love"}}                                                                                   
>>> course                                                                                                                                                                            
{'title': 'Python Collections', 'teacher': {'first_name': 'Kenneth', 'last_name': 'Love'}}                                                                                               
>>>    

the code doesn't print out with 'teacher' first but instead it starts with 'title'.

my question is why isn't it working and why does it work for Kenneth when he should have another } at the end of the code instead of one }

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The order of dict key / value pairs printed is dependent on an internal hashing of the key values. This order is not predictable and might even change as more keys are added to the dictionary. The order may even change for different versions of python due to changes in internal dict organization and hashing changes.

You are correct, in that the closing curly bracket should be required. I can't say why the REPL version Kenneth was using allowed the missing curly bracket. Perhaps, his version assumed auto closing of brackets. This is not standard.

Also, in his video "videos": 22 show up at the end of the dictionary but he doesn't call for it at all. how does that come about

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I believe he was only trying to show the different types of objects that can be used for a dict item value, and to show the different types of objects that can be used as a dict key.