在Python中,如果值是nan/null/empty,如何从字典的列表中删除key和value。我试着用下面的代码来删除,但它没有被删除。 在代码中是否有什么错误的做法。
如果值是nan/null/empty,我们需要键和值对。
Code tried
1st method tried
for key,value in address_dic.items():
if value is 'nan':
del address_dic[key]
2nd method tried
for key,value in dict(address_dic[address]).items():
if value is 'nan':
del address_dic[address][key]
地址_dic中的输入数据
{'source_id': 4402350, 'address': [{'address_type': 'Primary', 'address_line_1': '11815', 'address_line_2': nan}, {'address_type': 'work', 'address_line_1': nan, 'address_line_2': 123}]}
Expected Output
{'source_id': 4402350, 'address': [{'address_type': 'Primary', 'address_line_1': '11815'}, {'address_type': 'work', 'address_line_2': 123}]}