字典结果转换列表怎么转换

如何将字典遍历出来的结果转换为列表,然后将其放在if语句里面做判断


test_dict = {"a":"1","b":"2"}
test_list = []
for key in test_dict:
    test_list.append(key+"\t"+test_dict[key])
test_str = "a\t1"
print(test_str in test_list)

字典遍历出来的结果转换为列表?
字典是两部分组成:键和值
你是要那一部分转为列表?

直接

print(list(dic.items()))

就可以

字典有三个现成的方法转列表,看实际需要哪种形式:

>>> dct = {"a":1,"b":2,"c":3}
>>> dct.keys()
dict_keys(['a', 'b', 'c'])
>>> dct.values()
dict_values([1, 2, 3])
>>> dct.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> list(dct.keys())
['a', 'b', 'c']
>>> list(dct.values())
[1, 2, 3]
>>> list(dct.items())
[('a', 1), ('b', 2), ('c', 3)]