I am trying to find the largest and smallest word in a given string.
This is my target string.
'Hello, welcome to python programming '
I have extracted the words in the string and their lengths into key value pairs in a dictionary. Below is the code i wrote.
Line='Hello, welcome to python programming '
words=[x for x in Line.split()]
print(words)
lengths=[len(word) for word in words]
print(lengths)
x = {words[i]: lengths[i] for i in range(len(words))}
print('--'*44)
print (x)
I am getting the resulted dictionary as below
['Hello,', 'welcome', 'to', 'python', 'programming']
[6, 7, 2, 6, 11]
*************************************************************************
{'Hello,': 6, 'welcome': 7, 'to': 2, 'python': 6, 'programming': 11}
I want to sort the dictionary and print the keys which have the highest and lowest values i.e the words which are longest and shortest.