Hello all,

I need a little help with Dictionary statement. My question is it possible to display just one thing for a Dictionary ? I know that Dictionary use Keys and Values so if I have this code.

How would I display let say the Dog from Dictionary without printing the value in print statment and then show the value without showing Dog and so on. Can this be done. I am New to Python so please try explain fully what you are doing. Thankyou

# use a dictionary for key(items and cost valve of item)
dic = {'Dog' :10, 'Cat' :50, 'Boat' :35,}
# use tuffle for money that you got
m =(100)
# display items for sale

print ' I have the following items for sale '
for k, v in sorted(dic.items()):
   print k, ' at' ,v, ' money.\n' 
#buying a item and deducting money

Edited by FAITH2011: errors

7 Years
Discussion Span
Last Post by pyTony
dic = {'Dog' :10, 'Cat' :50, 'Boat' :35,}
print ' I have the following items for sale '
print ', '.join(dic)

thanks tonyjv for your reply, I have tried your code enter and it does print out the Dog, Cat and Boat keys for the dictionary, which is great but is there anyway of printing just one of them so I would get output like this.

I have the follow item for sale a Dog(Dictionary Key 1) at £ 10 (dicto
ionary Value (1).

I know this could be done Lists and tuffles but just wondering if Dictionary can to this too, so I use one bit of coding instead off two.
Once again Thankyou for your time and answer.

Edited by FAITH2011: n/a


You had that allready in your code:

dic = {'Dog' :10, 'Cat' :50, 'Boat' :35,}
print ' I have the following items for sale '
for it, (k, v) in enumerate(sorted(dic.items())):
    print '\t%i: %s at £ %i' % (it+1, k, v)

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.