0

Alright so I have the following dictionary of planets, and each planet has its own dictionary containing its specs:

d={
'Mercury':{
'Distance from the sun' : 58,
'Radius' : 2439.7,
'Gas planet?' : False,
'Atmosphere?' : True,
'Moons' : []},
'Jupiter':{
'Distance from the sun' : 483,
'Radius' : 69911,
'Gas planet?' : True,
'Atmosphere?' : True,
'Moons' : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']},
'Uranus':{
'Distance from the sun' : 3000,
'Radius' : 25559,
'Gas planet?' : True,
'Atmosphere?' : True,
'Moons' : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon']},
'Mars':{
'Distance from the sun' : 207,
'Radius' : 3396.2,
'Gas planet?' : False,
'Atmosphere?' : True,
'Moons' : ['Phobos', 'Deimos']},
'Earth':{
'Distance from the sun' : 150,
'Radius' : 6371.0,
'Gas planet?' : False,
'Atmosphere?' : True,
'Moons' : ['Moon']},
'Venus':{
'Distance from the sun' : 108,
'Radius' : 6051.8,
'Gas planet?' : False,
'Atmosphere?' : True,
'Moons' : []},
'Saturn':{
'Distance from the sun' : 1400,
'Radius' : 60268,
'Gas planet?' : True,
'Atmosphere?' : True,
'Moons' : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea']},
'Neptune':{
'Distance from the sun' : 4500,
'Radius' : 24764,
'Gas planet?' : True,
'Atmosphere?' : True,
'Moons' : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa']}}`

Essentially what I want to do is print it in the order it appears in the dictionary, so I used the code:

for planets in sorted(d.keys()):
print(planets)
    for k,v in sorted(d[planets].items()):
        print(k, ":", v)

However this yields a random order of each planet and key value for each planet's description. (The planet name and then its specs are printed underneath it when I run it in python, I just didn't know how to format it to show it that way on stack)

i.e.:

Neptune
Moons : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa']
Radius : 24764
Distance from the sun : 4500
Gas planet? : True
Atmosphere? : True
Jupiter
Moons : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']
Radius : 69911
Distance from the sun : 483
Gas planet? : True
Atmosphere? : True
Earth
Moons : ['Moon']
Radius : 6371.0
Distance from the sun : 150
Gas planet? : False
Atmosphere? : True
Mercury
Moons : []
Radius : 2439.7
Distance from the sun : 58
Gas planet? : False
Atmosphere? : True
Mars
Moons : ['Phobos', 'Deimos']
Radius : 3396.2
Distance from the sun : 207
Gas planet? : False
Atmosphere? : True
Uranus
Moons : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon']
Radius : 25559
Distance from the sun : 3000
Gas planet? : True
Atmosphere? : True
Venus
Moons : []
Radius : 6051.8
    Distance from the sun : 108
    Gas planet? : False
    Atmosphere? : True
    Saturn
    Moons : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea']
    Radius : 60268
    Distance from the sun : 1400
    Gas planet? : True
    Atmosphere? : True

I've tried using sorted() but that just puts it in alphabetical order. Any suggestions?

Murmel
  • 5,402
  • 47
  • 53
  • What order to you want? What do you mean by "order it appears in the dictionary"? Dictionaries don't save elements in any order. – Jared Apr 05 '13 at 03:24
  • 3
    You can't do that, dictionaries are not meant for order, they are meant for mapping and retrieval mainly. What you can do is build a list of tuples (x,y), where x is the key and y is the value. You can print out the list of tuples of keys and values in that order using a loop and then pass that list into `dict()`, then you will have the dictionary after you printed it in that order. – eazar001 Apr 05 '13 at 03:47
  • For your own implementation: http://stackoverflow.com/questions/60848/how-do-you-retrieve-items-from-a-dictionary-in-the-order-that-theyre-inserted – eazar001 Apr 05 '13 at 03:49
  • Ahh too bad, well the more you know I guess. Thanks for letting me know guys – user2146234 Apr 05 '13 at 04:27
  • You may be able to use `collections.OrderedDict`. It's easy enough to return a list sorted by one of the properties. eg. `sorted(d.items(), key=lambda (k,v):v['Distance from the sun'])` – John La Rooy Apr 05 '13 at 05:56

2 Answers2

0

This cannot be done. Python dictionaries do not make any promises about their printed order.

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
0

It's very simple(as a work around): Just put any of the keys in a list, sort it, and then access it element by element using it as a key to access and print your dictionary.

delas
  • 1