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.