Enumerate Keys in a dictionary?

dictionary, python-2.7

Solution

You can use `.keys()`:

for key in your_dict.keys():
  print key

or just iterate over the dictionary:

for key in your_dict:
  print key

Do note that dictionaries aren't ordered. Your resulting keys will come out in a somewhat random order:

['Wonderland', 'ALice', 'in']

If you care about order, a solution would be to use lists, which are ordered:

sort_of_dict = [('ALice', 1), ('in', 2), ('Wonderland', 3)]

for key, value in sort_of_dict:
  print key

Now you get your desired results:

>>> sort_of_dict = [('ALice', 1), ('in', 2), ('Wonderland', 3)]
>>> 
>>> for key, value in sort_of_dict:
...   print key
... 
ALice
in
Wonderland

Problem

I have a dictionary ``` Dict = {'ALice':1, 'in':2, 'Wonderland':3} ``` I could find ways to return key values but there was no way to return key names. I want Python to return the dictionary key names step by step (for loop) for example: ``` Alice in Wonderland ```

Original source