Search code examples
pythonpython-3.xdictionaryordereddictionary

filter out item from ordered dict


For the following ordered dictionary, how can I print just the 1)'Price' and its value 2) rank it in descending order with its corresponding 'room_id'

[OrderedDict([('room_id', '1133718'), ('survey_id', '1280'), ('host_id', '6219420'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK03'), ('reviews', '9'), ('overall_satisfaction', '4.5'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), ('latitude', '1.293354'), ('longitude', '103.769226'), ('location', '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F')]), OrderedDict([('room_id', '3179080'), ('survey_id', '1280'), ('host_id', '15295886'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'TS17'), ('reviews', '15'), ('overall_satisfaction', '5.0'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '77.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:24.216548'), ('latitude', '1.310862'), ('longitude', '103.858828'), ('location', '0101000020E6100000E738B709F7F659403F1BB96E4AF9F43F')]), OrderedDict([('room_id', '15303457'), ('survey_id', '1280'), ('host_id', '97053568'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK05'), ('reviews', '0'), ('overall_satisfaction', '0.0'), ('accommodates', '14'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '60.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:16.969900'), ('latitude', '1.333744'), ('longitude', '103.764612'), ('location', '0101000020E610000044882B67EFF0594093C7D3F20357F53F')])]

Solution

  • @Arjun, you can also try the below code to solve your problem.

    The solution uses the concept of list comprehension and sorted() function with one of its keyword argument key.

    from collections import OrderedDict
    
    dicts = [
        OrderedDict([
            ('room_id', '1133718'), ('survey_id', '1280'),
            ('host_id', '6219420'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'MK03'), ('reviews', '9'), 
            ('overall_satisfaction', '4.5'), ('accommodates', '12'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), 
            ('latitude', '1.293354'), ('longitude', '103.769226'), 
            ('location', '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F')]), 
        OrderedDict([('room_id', '3179080'), ('survey_id', '1280'), 
            ('host_id', '15295886'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'TS17'), ('reviews', '15'), 
            ('overall_satisfaction', '5.0'), ('accommodates', '12'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '77.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:24.216548'), 
            ('latitude', '1.310862'), ('longitude', '103.858828'), 
            ('location', '0101000020E6100000E738B709F7F659403F1BB96E4AF9F43F')]), 
        OrderedDict([('room_id', '15303457'), ('survey_id', '1280'), 
            ('host_id', '97053568'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'MK05'), ('reviews', '0'), 
            ('overall_satisfaction', '0.0'), ('accommodates', '14'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '60.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:16.969900'), 
            ('latitude', '1.333744'), ('longitude', '103.764612'), 
            ('location', '0101000020E610000044882B67EFF0594093C7D3F20357F53F')])];
    
    output = '\n'.join([ 'room_id: {}, price: {}'.format(
        item['room_id'], item['price']
    ) for item in sorted(dicts, key=lambda d: int(d['room_id']))])
    
    print(output);
    

    Output »

    room_id: 1133718, price: 74.0
    room_id: 3179080, price: 77.0
    room_id: 15303457, price: 60.0