You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中将二维列表转换为字典列表?

二维列表转指定字典列表的实现

这里提供两种简洁的实现方式:

方法一:列表推导式(推荐)

这种方式代码简洁高效,一行就能完成转换:

list1 = [["ab","cd"],["ef","gh"]]    
output_list_of_dict = [{"name": sub[0], "phone": sub[1]} for sub in list1]
print(output_list_of_dict)

方法二:普通循环遍历

如果需要更直观的执行逻辑,可以用循环逐个构建字典并添加到结果列表:

list1 = [["ab","cd"],["ef","gh"]]    
output_list_of_dict = []
for sub in list1:
    output_list_of_dict.append({"name": sub[0], "phone": sub[1]})
print(output_list_of_dict)

两种方法执行后都会输出目标结果:

[{"name": "ab", "phone":"cd"},{"name": "ef", "phone":"gh"}]

内容的提问来源于stack exchange,提问作者fardV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:15:48