MST

星途 面试题库

面试题:Python复杂结构下字典与列表的转换处理

给定一个复杂的字典 `complex_dict = {'data': [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]}`, 要求将 `complex_dict['data']` 中的每个字典提取出 `name` 作为键,`age` 作为值,形成一个新的字典,再将这个新字典转换为一个列表,列表元素为包含键值对的元组。请用Python代码实现。
20.3万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
complex_dict = {'data': [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]}
new_dict = {sub_dict['name']: sub_dict['age'] for sub_dict in complex_dict['data']}
result_list = list(new_dict.items())
print(result_list)