-1

I have this json file:

    [
      {"industry": "food", "price": 100.0, "name": "mcdonald's"}, 
      {"industry": "food", "price": 90.0, "name": "tacobell"}, 
      {"industry": "food", "price": 150.0, "name": "Subway"}, 
      {"industry": "Cars", "price": 90.0, "name": "Audi"}
    ]

This is my code:

import json
from pprint import pprint

with open('firm_list.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())
pprint(data)
    
result_list=[]
for json_dict in data:
    result_list.append(json_dict['price'])
result_list=[json_dict['price'] for json_dict in data]
result_list.sort(reverse= True)
print(result_list)

I want to print a list of firms in the food industry and respective prices, which is sorted by the price, with the highest price appearing first. But my code print in the list also the firm from the car industry. How can I print just the firms from the food industry? Is it possible to have the name of the firms on the list too?

dtc348
  • 309
  • 6
  • 19
Moly
  • 13
  • 4
  • `data.sort(reverse=True, key=lambda i: i['price']); print(data)`… – deceze Nov 11 '21 at 08:34
  • Welcome to Stack Overflow. Please read [ask] and, in the future, try to communicate your problems with the code more clearly. I was confused here because you have titled the question "how can I print a list?", but clearly you already have code that successfully prints lists. – Karl Knechtel Nov 11 '21 at 08:52

3 Answers3

1

You can add an if filter to your list comprehension:

result_list = [
    json_dict['price']
    for json_dict in data
    if json_dict['industry'] == 'food'
]
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • Thank you. Do you know how I can add the name of the firms to the list? – Moly Nov 11 '21 at 08:46
  • What one of the other answers wrote... https://stackoverflow.com/a/69925352/683329 – Jiří Baum Nov 11 '21 at 09:26
  • Yes, but that code doesn't print the result from the highest to the lowest as I would like. – Moly Nov 11 '21 at 13:35
  • Right, they've updated their answer to do that now. In general, it's like Lego - solve the problems by putting together small pieces, rather than in one big chunk... – Jiří Baum Nov 11 '21 at 22:26
1

You can filter by food industry then sort the result:

import json

with open('example.json') as data_file:
    data = json.loads(data_file.read())

data = [i for i in data if i['industry'] == 'food']
data.sort(key=lambda i: i['price'], reverse=True)
# EDIT: filtering keys in dictionaries
data = [{k: d[k] for k in ['name', 'price']} for d in data]
print(data)

Result:

[{'price': 90.0, 'name': 'tacobell'}, {'price': 100.0, 'name': "mcdonald's"}, {'price': 150.0, 'name': 'Subway'}]
ChrisOram
  • 1,254
  • 1
  • 5
  • 17
  • And how can I have the result printed from the highest price to the lowest? – Moly Nov 11 '21 at 13:33
  • use `data.sort(key=lambda i: i['price'], reverse=True)`, see https://docs.python.org/3/howto/sorting.html – ChrisOram Nov 11 '21 at 13:38
  • Thank you. And what about if I want to print just the price and the name, but not the industry? Do you have any documents to recommend? – Moly Nov 11 '21 at 13:52
  • At this point `data` just holds JSON, you can read up about the structure of JSON online. In python, your JSON is serialized into a list of dictionaries see [python JSON docs](https://docs.python.org/3/library/json.html). That way you can loop through each of the dictionaries and pull out the price and the name. – ChrisOram Nov 11 '21 at 16:11
  • `data = [{k: d[k] for k in ['name', 'price']} for d in data]` should do it. – ChrisOram Nov 11 '21 at 16:20
  • Can you put this into your code? Because if I write data = [{k: d[k] for k in ['name', 'price']} for d in data], I have an error when I put the if statement to have ['industry']=='food' – Moly Nov 11 '21 at 16:37
  • @Moly see the edit in the answer. – ChrisOram Nov 11 '21 at 16:55
  • 1
    Thank you so much! – Moly Nov 11 '21 at 17:33
0

In the line 9 of the code, you are appending all the price to result_list. You might want to add a check. append that price to the results only if json_dict['industry'] == 'food'

result_list=[]

for json_dict in data:
    if (json_dict['price']):
        result_list.append(json_dict['price'])

result_list.sort(reverse= True)
print(result_list)

Also, result_list=[json_dict['price'] for json_dict in data] seems like a redundant line to me. I don't think it's required there.

Kumar Gaurav
  • 163
  • 2
  • 10