1

I have a newbie question regarding the usage of comma (,) when trying to get the key from a dictionary. I checked Python 3.7 documentation,and several other sites but couldn't find the answer.

I have a simple dictionary with one element in it:

basket = {'apple':1}

I want to get string 'apple' from it. If I do:

k = basket.keys()
print(k)

I get

dict_keys(['apple'])

If I do:

k, = basket.keys()
print(k)

I get

apple

So why do I need the comma to get the string 'apple'?

l001d
  • 723
  • 9
  • 15
  • 1
    Because the method is `keys()`, plural; it returns a collection of keys. It so happens that your dict only has one key, but it still returns a collection. – Daniel Roseman Jul 19 '19 at 12:23

0 Answers0