![]() |
VOOZH | about |
We are given a key value list dictionary we need to convert it list of lists. For example we are given a dictionary a = {'name': 'Geeks', 'age': 8, 'city': 'Noida'} we need to convert this into list of lists so the output should be [['name', 'Geeks'], ['age', 25], ['city', 'Geeks']].
List comprehension iterates over each key-value pair in the dictionary, extracting both the key and its corresponding value. These pairs are then collected into sublists, resulting in a list of lists.
[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
map()map()function applies a lambda to each key-value pair in the dictionary, converting each pair into a list. The result is then converted to a list, creating a list of lists with key-value pairs.
[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
dict.items() and list()dict.items() method provides the dictionary's key-value pairs as tuples. Using list(), these pairs are converted into a list of lists, where each list contains a key-value pair.
[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
map(list, a.items()) applies the list() function to each key-value pair (tuple) from the dictionary a, converting each tuple into a list of the form [key, value].list() function collects the results from map() into a list of lists, which is then printed as the final output.