diff --git a/S/Sort Words in a file according to their length in a list/Code.py b/S/Sort Words in a file according to their length in a list/Code.py new file mode 100644 index 00000000..53c5fcc1 --- /dev/null +++ b/S/Sort Words in a file according to their length in a list/Code.py @@ -0,0 +1,17 @@ +def create_dict(file_path): + with open(file_path, 'r') as f: + paragraph = f.read() + + words = paragraph.split() + word_dict = {} + for word in words: + word_length = len(word) + if word_length not in word_dict: + word_dict[word_length] = [] + word_dict[word_length].append(word) + return word_dict + +file_path = input("Enter the file path: ") +word_dict = create_dict(file_path) +print("Dictionary with word length as key and words with that length as value:") +print(word_dict)