In this python dictionary program, we will convert a key value list dictionary into a list of lists.
Steps to solve the program
- Take a dictionary as input and create an empty list.
- Use for loop to iterate over keys and values of the input dictionary.
- Create an empty list inside the for loop.
- First, add the key to the empty list using append().
- Now using a nested for loop iterate over a list of values and add them to the empty list using append().
- Now add this list to the first empty list.
- Repeat this process for each key-value pair.
- Print the output.
dict1 = {'sqa':[1,4,6],'tools':[3,6,9]}
list1 = []
for key,val in dict1.items():
l = []
l.append(key)
for ele in val:
l.append(ele)
list1.append(list(l))
print(list1)
Output :
[['sqa', 1, 4, 6], ['tools', 3, 6, 9]]