Given a list of dictionaries that contain information about shops:
shops = [
{"name": "Barneys New York", "state": "NY"},
{"name": "Bealls", "state": "CO"},
{"name": "Belk.", "state": "PA"},
{"name": "Boscov's.", "state": "AL"},
{"name": "Dillard's.", "state": "OH"},
{"name": "Hudson's Bay Company", "state": "NY"},
{"name": "Lord & Taylor", "state": "CA"},
{"name": "Saks Fifth Avenue", "state": "NY"},
{"name": "J. C. Penney.", "state": "MD"},
{"name": "Kohl's.", "state": "VA"},
]
If you want to iterate of the list, you don't need an iterator counter. Calling the variable you want to iterate over will give you each element one at a time.
for shop in shops:
The following lines would accomplish the same thing, but it's a hell of a lot more difficult to read and introduces a lot of unnecessary operations. The shops would already be iterated through to generate the len()
, then you are creating another iterator range()
. Finally, you're indexing something that could have just easily been passed to you. So yeah, use the simpler way to loop.
for i in range(len(shops)):
shop = shops[i]
Now, if you want to find if that shop is in a given state, you can check to see if it's in one of the dictionaries keys by using if <state> in shop.values()
or if you know you're looking for the state key/value pair, you can evaluate the dictionary key directly with shop['state']
. If that matches the state you want, just print out what you need.
for shop in shops:
if shop['state'] == 'CA':
print(shop['name'])
# Lord & Taylor
At this point, you can access any of the portions of the shop's dictionary and do any valid logic.
With your code, you are better off putting everything in a list right off the bat. If you are receiving information in a given way, then you'll have to deal with it, but if you're hard coding it into your file like this, then go ahead and turn this:
dict_A = {'name':'A', 'goods':'clothing', 'price':200}
dict_B = {'name':'B', 'goods':'interior', 'price':180}
dict_C = {'name':'C', 'goods':'clothing', 'price':50}
shops_all = [dict_A, dict_B, dict_C]
into this
shops_all = [
{'name':'A', 'goods':'clothing', 'price':200},
{'name':'B', 'goods':'interior', 'price':180},
{'name':'C', 'goods':'clothing', 'price':50},
]
Now, let's say you want to find clothing and you only have $100.
choice = 'clothing'
price = 100
Now I'm assuming you were intending budget to be the max you wanted to spend, so you have a logic error in your if price <= item['price']
line. Here you are looking where the store price is higher than what you want. So lets flip that to item['price'] <= price
. You also had a small error where you put item['shop']
when you meant item['name']
based on your sample data. Once those are fixed, you can give it a shot.
for item in shops_all:
if choice == item['goods']:
if item['price'] <= price:
print('shop {}, price {} dollars'.format(item['name'], item['price']))
else:
print('no such shop')
# no such shop
# shop C, price 50 dollars
Now you see the "no such shop" message showing up which is clearly not the case. There are two issues with the code here. 1) the else should be dedented to be with the first if statement and 2) it would still pritnt for shop B. So, this is where a flag variable success
could be useful. You can set the variable to false, and flip it to true when you have a match. Then check for it after your done iterating if you want to print a message letting them know they're out of luck. Also, it would be good to check the choice and price at the same time using and
.
success = False
for item in shops_all:
if choice == item['goods'] and item['price'] <= price:
success = True
print('shop {}, price {} dollars'.format(item['name'], item['price']))
if not success:
print('no such shop')
# shop C, price 50 dollars