3. Why is the order like it is?



I don't get error messages but I want to understand why the code gives that output?

I expected to print it out in order of the dictionary. Like this:
Name Dave
Age 22
Me male

but it prints out:

Age 22
Name Dave
Me male


my_dict = {
    "Name": "Dave",
    "Age": 22,
    "Me": "male"
for key in my_dict:
    print key, my_dict[key]


dictionary's aren't ordered in python. List are, dictionary's are not. as you can read here


thank you for the fast answer!!


i like on of the comments in the stackoverflow question:
Unordered means order is none of your business. It does NOT mean the order is inconsistent

Python is written in C, I have no idea how a dictionary looks under the python hood, maybe it is a linked list? Who knows. Either way, there is order, but the order is not for you to determine. If you want to control the order, you will have to use a nested list