How do you find the first key in a dictionary?

前端 未结 12 910
清酒与你
清酒与你 2020-11-27 10:58

I am trying to get my program to print out \"banana\" from the dictionary. What would be the simplest way to do this?

This is my dictionary:

         


        
12条回答
  •  野性不改
    2020-11-27 11:38

    easiest way is:

    first_key = my_dict.keys()[0]
    

    but some times you should be more careful and assure that your entity is a valuable list so:

    first_key = list(my_dict.keys())[0]
    

提交回复
热议问题