4

My code is the following,

prices = {'banana':'4', 'apple':'2', 'orange':'1.5', 'pear':'3'}
stock = {'banana':6, 'apple':0, 'orange':32, 'pear':15}

print(prices.keys)
print("price :" + str(prices.values))
print("stock :" + str(stock.values))

I don't understand why I'm getting a spit out that looks as if I asked for the type. What gives?

Actually, my code logic is wrong.

I want the code to spit out the following

key price : values stock : values

For Example, this is how it should look

apple price: 2 stock: 0

5 Answers5

8

You need to call the methods to get anything useful:

print (prices.keys())

However, in python3.x, this still isn't particularly nice for printing since it'll print extra junk that you probably don't want to see.

You might want to consider using str.join on the object returned from dict.keys() or dict.values():

print (' '.join(prices.keys()))

str.join does pretty much what you'd expect it to. The string on the left is the delimiter that gets inserted between each element in the iterable you pass to join. For example:

"!".join(["foo","bar","baz"])

will result in the string: "foo!bar!baz". The only gotcha here is that each element in the iterable that you pass to str.join must be a string.


As for your edit,

prices = {'banana':'4', 'apple':'2', 'orange':'1.5', 'pear':'3'}
stock = {'banana':6, 'apple':0, 'orange':32, 'pear':15}
prices.keys() & stock.keys()  #{'orange', 'pear', 'banana', 'apple'}
for item in (prices.keys() & stock.keys()):
    print (item,"price:",prices[item],"stock:",stock[item])

which outputs:

orange price: 1.5 stock: 32
pear price: 3 stock: 15
banana price: 4 stock: 6
apple price: 2 stock: 0

seems like it is what you want.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thank you. I'm not familiar with join. I'll look it up now. –  Jan 22 '13 at 21:46
  • thank you but even with the fix, the code is not doing what I thought it would. Could you take a look at my edit? –  Jan 22 '13 at 21:50
  • @algebr -- I saw you edit. I'm not sure that I understand what you expect the output to be. Could you clarify the post showing exactly what you want the output to be? – mgilson Jan 22 '13 at 21:51
  • Awesome! thank you so much again. but line 3 of your code, what does it do? it just seems like its floating..? –  Jan 22 '13 at 21:55
  • @algebr -- It is floating. I put it there to demonstrate what `dict1.keys() & dict2.keys()` does. In this case, it really does pretty much the same thing as `dict1.keys()` since your dictionarys have the same keys -- If they didn't, it would return the intersection of the two sets of keys. – mgilson Jan 22 '13 at 22:02
  • or just `print(' '.join(prices))` instead of calling `.keys()` – John La Rooy Jan 22 '13 at 22:31
  • @gnibbler -- Very true. I wrote that back when I was assuming OP wanted to do the values as well. – mgilson Jan 22 '13 at 23:08
  • As alluded to in this answer, `dict.keys()` returns an object of type `dict_keys`, not an index-able object like a list of strings. To get a list of the keys as strings, you should simply use `list(dict)`. – Jess Riedel Jul 21 '18 at 17:58
4
prices = {'banana':'4', 'apple':'2', 'pear':'3'}
stock = {'banana':6, 'orange':32, 'pear':15}
for item in (prices.keys() & stock.keys()):
    print (item,"price:",prices.get(item,'-'),"stock:",stock.get(item,0))

Produces

orange price: - stock: 32
pear price: 3 stock: 15
banana price: 4 stock: 6
apple price: 2 stock: 0

Using get with a default will help if the stock and prices dictionaries contain different friuts ('keys') in each. The .get() function really helps here.

As mentioned by mgilson the following line is where the full set of fruit is created.

prices.keys() & stock.keys()  #{'orange', 'pear', 'banana', 'apple'}

I've also done this using set before

set(prices.keys().extend(stock.keys())

But I prefer the & approach.

Matt Alcock
  • 12,399
  • 14
  • 45
  • 61
  • Why does the orange price not come up but the apple does? –  Jan 23 '13 at 04:44
  • Ah, nevermind, I just used | instead of & and got my desired result. Thank you again! –  Jan 23 '13 at 04:46
1

The top two answers recommend using .keys(), but note that this returns an object of type dict_keys, not an index-able object like a list of strings. To get a list of the keys as strings, you should simply cast the dictionary as a list: list(my_dict).

>>> prices = {'banana':'4', 'apple':'2', 'pear':'3'}
>>> print(prices.keys())
dict_keys(['banana', 'apple', 'pear'])
>>> prices.keys()[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
>>> print(list(prices))
['banana', 'apple', 'pear']
>>> list(prices)[2]
'pear'
Jess Riedel
  • 336
  • 3
  • 15
0

Since both dictionaries/objects have the same keys you can use a simple for loop to iterate over one of the dictionaries and print the price and stock of each item.

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock  = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for key in prices:
    print "stock: %s" % stock[key]
    print "prices: %s" % prices[key]
depperm
  • 10,606
  • 4
  • 43
  • 67
andrey
  • 1
0
prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock  = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for key in prices:
    print key
    print "stock: %s" % stock[key]
    print "prices: %s" % prices[key]

which outputs:

orange
stock: 32
prices: 1.5
pear
stock: 15
prices: 3
banana
stock: 6
prices: 4
apple
stock: 0
prices: 2
=> None
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
andrey
  • 1