W3docs

Converting Dictionary to List?

In Python, you can convert a dictionary to a list of its keys or values using the keys() and values() methods, respectively.

In Python, you can convert a dictionary to a list of its keys or values using the keys() and values() methods, respectively. Here's an example:

Convert a dictionary to a list of its keys or values using the "keys()" and "values" methods in Python

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

If you want to convert both keys and values of the dictionary to a list of tuples, you can use the items() method and pass each tuple to the list() constructor:

Convert both keys and values of the dictionary to a list of tuples using the items() method in Python

# Convert to a list of tuples (keys and values)
items_list = list(my_dict.items())
print(items_list)  # Output: [('a', 1), ('b', 2), ('c', 3)]

If you want to convert the dictionary to List of list where each list will have key and value then you can use list comprehension

Convert the dictionary to List of list where each list will have key and value then you can use list comprehension in Python

items_list = [[k,v] for k,v in my_dict.items()]
print(items_list)  # Output: [['a', 1], ['b', 2], ['c', 3]]

You can also use dict.items() method with map function

using items() method with map function

items_list = list(map(lambda item: list(item), my_dict.items()))
print(items_list)  # Output: [['a', 1], ['b', 2], ['c', 3]]