3

When I do:

d = {'x': 1, 'y': 2, 'z': 'randy'}

print(d.keys())

The result is:

dict_keys(['x', 'y', 'z'])

Why is there the dict_keys? Is there a way to get rid of it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Roger Tan
  • 41
  • 1
  • 6
  • `print(list(d.keys())`, or `print(" ".join(str(key) for key in d.keys()))`, depending on what output format you'd like. –  Jul 22 '17 at 10:06
  • See also https://stackoverflow.com/questions/7296716/what-is-dict-keys-dict-items-and-dict-values , for the how and why. –  Jul 22 '17 at 10:07
  • 1
    The short answer is: because it's a dict_keys object. – jonrsharpe Jul 22 '17 at 10:14

1 Answers1

8

Convert it to a list.

>>> print(list(d.keys()))
['z', 'x', 'y']
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358