从字典列表中获取键和值

时间:2013-03-07 16:54:23

标签: python list dictionary key

我有一个包含许多词典的列表,我需要为每个词典获取键和值:

[{'keys1.1' = 'value1.1','key1.2'='values1.2'},{'keys2.1'='value2.1'},{'key3.1'='value3.1'}]

我如何获得这些键和值?

1 个答案:

答案 0 :(得分:0)

您需要遍历dict中的for-loop

list_of_dicts = [{'keys1.1' : 'value1.1',
                   'key1.2':'values1.2'},
                 {'keys2.1':'value2.1'},
                 {'key3.1':'value3.1'}]


for item in list_of_dicts:
  for key, value in item.iteritems()
    print key, value
    print "The key is {0} with a value of {1}".format(key, value)
#or
for item in list_of_dicts:
  for key in item.keys():
    print item[key]
    print "The key is {0} with a value of {1}".format(key, item[key])