In this python dictionary program, we will group the elements of a list based on the function.
Steps to solve the program
- Take a list as input and create an empty list.
- Use for loop to iterate over the words in the dictionary.
- Add the word length as the key using the len() function and the word as its value to the empty dictionary.
- Print the output.
l = ['abc','defg','hijkl']
D1 = {}
for val in l:
D1[len(val)] = val
print(D1)
Output :
{3: 'abc', 4: 'defg', 5: 'hijkl'}