Is there a way to print or extract just the "key(s)" in a dictionary


#1

Is there a way to print or extract just the "key(s)" in a dictionary ?


#2

Of course, just use keys() method:
an example >

print dictionary.keys()


#3

You could try the .keys() method.

>>> ask
{'akin': 'today', 'bobo': 'next'}
>>> list(ask.keys())
['akin', 'bobo']

The only issue is that it may not be in the order. To do that, use the sorted() method.

sorted(ask.keys())

Hope this helps! :slight_smile:


#4

So if your dictionary is apples, you print apples.keys()


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.