Question: Problem 4 Given the dictionary states where the key is the state and the value is the population. Sort states on population and print the
Problem 4
Given the dictionary states where the key is the state and the value is the population. Sort states on population and print the three states with the highest population and the three states with the lowest population.
In [59]:
states = {"California":39536653,"Texas":28304596,"Florida":20984400,"New York":19849399,"Pennsylvania":12805537, "Illinois":12802023,"Ohio":11658609,"Georgia":10429379,"North Carolina":10273419,"Michigan":9962311,
"New Jersey":9005644,"Virginia":8470020,"Washington":7405743,"Arizona":7016270,"Massachusetts":6859819,
"Tennessee":6715984,"Indiana":6666818,"Missouri":6113532,"Maryland":6052177,"Wisconsin":5795483,
"Colorado":5607154,"Minnesota":5576606,"South Carolina":5024369,"Alabama":4874747,"Louisiana":4684333,
"Kentucky":4454189,"Oregon":4142776,"Oklahoma":3930864,"Connecticut":3588184,"Puerto Rico":3337177,
"Iowa":3145711,"Utah":3101833,"Arkansas":3004279,"Nevada":2998039,"Mississippi":2984100,"Kansas":2913123,
"New Mexico":2088070,"Nebraska":1920076,"West Virginia":1815857,"Idaho":1716943,"Hawaii":1427538,
"New Hampshire":1342795,"Maine":1335907,"Rhode Island":1059639,"Montana":1050493,"Delaware":961939,
"South Dakota":869666,"North Dakota":755393,"Alaska":739795,"District of Columbia":693972,"Vermont":623657,
"Wyoming":579315}
# write your code below
for state in states:
sorted_dict = {} sorted_keys1 = sorted(states, reverse = True, key=states.get)
for w in sorted_keys1:
sorted_dict[w] = states[w]
val = sorted_dict.keys()
If you could use lambda that would be helpful. I got it sorted with sorted function I just don't know where to go from there. Comments would be helpful for explanation too.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
