How to convert a dictionary to a list in Python?

You can convert a dictionary into a list using the items() method.

The sample code is shown below:

my_dict = {'a': 1, 'b': 2, 'c': 3}

# 使用 items() 方法将字典转化为列表
my_list = list(my_dict.items())

print(my_list)

The output result is:

[('a', 1), ('b', 2), ('c', 3)]

Each key-value pair from the dictionary is converted into a tuple and added as an element in the list.

bannerAds