
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Shortest Words in a Text File Using Python
In this article, we will show you how to print all the shortest words from a given text file using python. The shortest words are the words which are having the same length as the shortest word (minimum length) in a text file.
Assume we have taken a text file with the name TextFile.txt consisting of some random text. We will return all the shortest words from a given text file.
TextFile.txt
Good Morning Tutorials Point This is the Tutorials Point sample File Consisting of Specific abbreviated source codes in Python Seaborn Scala Imagination Summary and Explanation Welcome user Learn with joy
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Create a variable to store the path of the text file.
Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here "r" represents read-only mode).
with open(inputFile, 'r') as filedata:
Create a variable to read the text file data using the read() function(reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file) and split it into a list of words of a given text file using the split() function(splits a string into a list. We can define the separator; the default separator is any whitespace).
Find the length of the shortest word using the len()(The number of items in an object is returned by the len() method. It returns the number of characters in a string when the object is a string) and min()(returns the lowest-valued item in an iterable)functions from the above words list.
len(min(words List, key=len))
Using the list comprehension, get all the words with the shortest length and save them in another variable. Here we are traversing each word of the file and checking whether the length of that word is equal to the length of the shortest word using the for loop in a list comprehension.
The key=len specifies that we must obtain the word depending on its length, and we will obtain the minimum length word using the min() function and the length of the minimum length word using the len() function.
list comprehension:
Print all the shortest words from a given text file.
Close the input file with the close() function(used to close an opened file).
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Example
The following program checks the length of the shortest word and prints all the words which are having the same length as that of the shortest word from a given text file ?
# input text file inputFile = "ExampleTextFile.txt" # Opening the given file in read-only mode. with open(inputFile, 'r') as filedata: # Getting the list of words from a file wordsList = filedata.read().split() # finding the length of the shortest word in the above words list shortestWordLength = len(min(wordsList, key=len)) # Storing all the words having the minimum length(shortest word length) result = [textword for textword in wordsList if len(textword) == shortestWordLength] # Print the shortest words from a text file print("The following are the shortest words from a text file:") print(result) # Closing the input file filedata.close()
Output
On executing, the above program will generate the following output ?
The following are the shortest words from a text file: ['is', 'of', 'in']
We read some random text from a text file in this program. We read over the entire file and break it down into words. We determined the length of the minimum length word after we obtained the words. Then we went through the file word by word, checking whether the length of the corresponding word was equal to the length of the minimal length word. If it is true, we will print those words and close the open file.