遍历映射到单个键的值列表以创建新列表

时间:2020-04-02 21:25:04

标签: python python-3.x list loops dictionary

提供了字典:

somedictionary = {"a":["b","c","d"],"1":["2","3"]}

如何创建单独的列表,使['a', 'b', 'c', 'd']['1', '2', '3']是两个单独的列表?我的麻烦是遍历键值列表。

1 个答案:

答案 0 :(得分:2)

您可以使用items

w_dict = {"a":["b","c","d"],"1":["2","3"]}

lists = [[k] + v for k,v in w_dict.items()]

产量:

[['a', 'b', 'c', 'd'], ['1', '2', '3']]