3. Why is the order like it is?


#1



https://www.codecademy.com/courses/python-beginner-en-KAgt5/0/3#

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

why?


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


#2

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


#3

thank you for the fast answer!!


#4

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