How to verify only the elements names in a dictionary object?

时间:2016-10-09 15:49:33

标签: python python-2.7 python-3.x

I have a VERY big dictionary in python and when I type dict.items(dic_name) it shows up so many stuff (matrix, vectors, etc). I just would like to see the elements names. What should I do?

For example when I have a list I type names (my.list) and it just displays the list elements name. Is there a command like that in Python?

1 个答案:

答案 0 :(得分:1)

好吧,如果你只有钥匙,请问钥匙,而不是物品:

my_dict.keys()

如果您严格要求它是一个列表,您可以这样做:

my_list = list(my_dict.keys())

现在,如果你想迭代它们:

在python 2中:

for k in my_dict.iterkeys():
    do_something_with(k)

在python 3中:

for k in my_dict.keys():
    do_something_with(k)