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 '
print
for k, v in sorted(dic.items()):
   print k, ' at' ,v, ' money.\n' 
#buying a item and deducting money

Recommended Answers

dic = {'Dog' :10, 'Cat' :50, 'Boat' :35,}
print ' I have the following items for sale '
print ', '.join(dic)
Jump to Post

All 3 Replies

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.
Stephen

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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.