Search code examples
python-3.xdictionarykey-value-store

sort output of dict values and print the dict with sorted values


i tried printing the dict key's after sorting out by the below code. it worked fine

import pprint
from operator import itemgetter

cars = [
    {'Name':'Ka','Year Introduced':1996,'Production of the current model':2014,'Generation':'3rd Generation','Vehicle Information':'Developed by Ford Brazil as a super mini car'},
    {'Name':'Fiesta','Year Introduced':1976,'Production of the current model':2017,'Generation':'7th Generation (Mark VIII)','Vehicle Information':'Ford\'s long running subcompact line based on global B-car Platform'},
    {'Name':'Focus','Year Introduced':1998,'Production of the current model':2018,'Generation':'3rd Generation (Mark III)','Vehicle Information':'Ford\'s Compact car based on global C-car platform'},
    {'Name':'Mondeo','Year Introduced':1992,'Production of the current model':2012,'Generation':'2nd Generation (Fusion)','Vehicle Information':'Mid sized passenger sedan with "One-Ford" design based on CD4 platform.'},
    {'Name':'Taurus','Year Introduced':1986,'Production of the current model':2009,'Generation':'6th Generation','Vehicle Information':'Full sized car based on D3 platform'},
    {'Name':'Fiesta ST','Year Introduced':2013,'Production of the current model':2013,'Generation':'1st Generation (6th Generation)','Vehicle Information':'Fiesta\'s high performance factory tune'},
    {'Name':'Focus RS','Year Introduced':2015,'Production of the current model':2015,'Generation':'1st Generation (3rd Generation)','Vehicle Information':'Special high performance Focus developed by SVT'},
    {'Name':'Mustang','Year Introduced':1964,'Production of the current model':2014,'Generation':'6th Generation','Vehicle Information':'Ford\'s long running pony/muscle car'},
    {'Name':'GT','Year Introduced':2004,'Production of the current model':2016,'Generation':'2nd Generation','Vehicle Information':'Ford\'s limited production super car inspired by the legendary race car GT40'}
    ]

# Creates a new dictionary with the name value as the key, and the dictionary as the value   
def newDict(carlist):
    mynewDict ={}
    for car in carlist:
        mynewDict[car['Name'],car['Year Introduced']] = car

    return mynewDict


def alphaCars(mycardict):
    carlist = list(mycardict.keys())
    carlist.sort() 
    #print (carlist)
    return carlist


mydict = newDict(cars)

#pprint.pprint (alphaCars(mydict))



for i in sorted (newDict(cars).keys()):  
    print(i) 

i tried the same to get the sorted "value" from dict. tried single line code mentioned in stack overflow and i am unable to do so. getting the below mentioned error

Error : not supported between instances of 'dict' and 'dict

i have created dict from instances of another dict .i did not use lambda, it is straight forward to get the desired output with lambda. Wanted to check is there away to sort dict values with out lambda.


Solution

  • Thanks the following statement fixed it "newlist = sorted(alphaCars(mydict), key=itemgetter(1),reverse=True)" . Thanks all for your help