Kevin H.
Kevin H.

Reputation: 25

Python - How should I keep the key,value pairs in order when I print all of them?

I am brand new to coding and the project I am working on converts the user's weight on Earth into their weight on different planets of the solar system. I would like some suggestions on how I can make the output maintain the order in which I entered the elements into the dictionary.

Forgive my ignorance as I am trying to teach myself and I have just started with Python. Any guidance would be greatly appreciated.

earthWeight = float(input("How much do you weigh (in lbs)?: "))

#user's weight is calculated based on the planet.

sunWeight = earthWeight * 27.01

mercuryWeight = earthWeight * .38

venusWeight = earthWeight * .91

moonWeight = earthWeight * .166

marsWeight = earthWeight * .38

jupiterWeight = earthWeight * 2.34

saturnWeight = earthWeight * 1.06

uranusWeight = earthWeight * .92

neptuneWeight = earthWeight * 1.19

plutoWeight = earthWeight * .06

celestialWeight = {
    'Weight on Sun: ' : sunWeight, 
    'Weight on Mercury: ' : mercuryWeight,
    'Weight on Venus: ' : venusWeight,
    'Weight on Moon: ' : moonWeight,
    'Weight on Mars: ' : marsWeight,
    'Weight on Jupiter: ' : jupiterWeight,
    'Weight on Saturn: ' : saturnWeight,
    'Weight on Uranus: ' : uranusWeight,
    'Weight on Neptune: ' : neptuneWeight,
    'Weight on Pluto: ' : plutoWeight}


for key, value in sorted(list(celestialWeight.items())):
    print(key, value)

Upvotes: 0

Views: 256

Answers (4)

SuperCiocia
SuperCiocia

Reputation: 1971

It depends on what you mean by "order":

for key, value in celestialWeight.items():
   print(key, value)

will output them in the order you wrote them in:

Weight on Sun:  4861.8
Weight on Mercury:  68.4
Weight on Venus:  163.8
Weight on Moon:  29.880000000000003
Weight on Mars:  68.4
Weight on Jupiter:  421.2
Weight on Saturn:  190.8
Weight on Uranus:  165.6
Weight on Neptune:  214.2
Weight on Pluto:  10.799999999999999

while

for key, value in sorted(list(celestialWeight.items())):
   print(key, value)

will output them in alphabetical order:

Weight on Jupiter:  421.2
Weight on Mars:  68.4
Weight on Mercury:  68.4
Weight on Moon:  29.880000000000003
Weight on Neptune:  214.2
Weight on Pluto:  10.799999999999999
Weight on Saturn:  190.8
Weight on Sun:  4861.8
Weight on Uranus:  165.6
Weight on Venus:  163.8

If you to change the order a posteriori, then you can do it like so:

keys = list(celestialWeight.keys())
desired_order = [9,8,7,6,5,4,3,2,1,0]
reordered_dict = {keys[k]: celestialWeight[keys[k]] for k in desired_order_list}

which gives an inverted list:

{'Weight on Pluto: ': 10.799999999999999,
 'Weight on Neptune: ': 214.2,
 'Weight on Uranus: ': 165.6,
 'Weight on Saturn: ': 190.8,
 'Weight on Jupiter: ': 421.2,
 'Weight on Venus: ': 163.8,
 'Weight on Mars: ': 68.4,
 'Weight on Moon: ': 29.880000000000003,
 'Weight on Mercury: ': 68.4,
 'Weight on Sun: ': 4861.8}

Upvotes: 1

Kuljot Singh
Kuljot Singh

Reputation: 1

Just remove the 'sorted' function that you have used, and then the output will maintain the order of the dictionary as provided by you.

Upvotes: 0

CaptainDanila
CaptainDanila

Reputation: 38

Dictionaries compared to other data collections (list, tuples) don't use indexing, they however keep the order of the elements as they were added in the dictionary.

The reason you currently obtain a different result from what you expect is because you're applying the following:

sorted(list(celestialWeight.items())

the dictionary.items() function already returns a tuple formed of (key,pair), you're also converting it into a list and then sort it (since we're talking about keys which are strings) as per ASCII values.

All you need to do is:

for key, value in celestialWeight.items():
    print(key, value)

Upvotes: 0

rudiejd
rudiejd

Reputation: 392

You are sorting the dictionary before you print it. Change sorted(list(celestialWeight.items())) to list(celestialWeight.items()) if you want the output in insert order. Additionally, I don't think it is necessary to turn the items into a list as they are already iterable - this only introduces unnecessary overhead. You could even do just celestialWeight.items()

Upvotes: 0

Related Questions