Group the elements of a list based on the function.

In this python dictionary program, we will group the elements of a list based on the function.

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Use for loop to iterate over the words in the dictionary.
  3. Add the word length as the key using the len() function and the word as its value to the empty dictionary.
  4. 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'}
				
			

get the total length of a dictionary with string values.

retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Leave a Comment