Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Find the Shortest Words in a Text File using Python?
In this article, we will show you how to find and print all the shortest words from a given text file using Python. The shortest words are those having the same length as the minimum length word in the file.
We'll work with a sample text file containing various words of different lengths and extract all words that match the shortest length.
Sample Text File
Let's assume we have a text file called TextFile.txt with the following content ?
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
Following are the steps to find the shortest words ?
Open the text file in read-only mode using the
open()functionRead the file content and split it into a list of words using
split()Find the length of the shortest word using
min()withkey=lenUse list comprehension to collect all words matching the shortest length
Display the results
Example
The following program finds and prints all the shortest words from a text file ?
# Create sample text file content for demonstration
sample_text = """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"""
# Write sample content to a file
with open("sample_file.txt", 'w') as f:
f.write(sample_text)
# Read the file and find shortest words
with open("sample_file.txt", 'r') as filedata:
# Getting the list of words from the file
words_list = filedata.read().split()
# Finding the length of the shortest word
shortest_word_length = len(min(words_list, key=len))
# Getting all words with the minimum length using list comprehension
shortest_words = [word for word in words_list if len(word) == shortest_word_length]
# Print the results
print("Length of shortest word:", shortest_word_length)
print("The shortest words from the text file:")
print(shortest_words)
Length of shortest word: 2 The shortest words from the text file: ['is', 'of', 'in']
How It Works
The program works by ?
Reading the file: We use
open()with read mode andread().split()to get all wordsFinding minimum length:
min(words_list, key=len)returns the shortest word, andlen()gives its lengthFiltering words: List comprehension checks each word's length against the minimum length
Collecting results: All words matching the shortest length are stored in a list
Alternative Approach
You can also use a traditional loop instead of list comprehension ?
# Alternative method using a regular loop
sample_text = "Good Morning Tutorials Point This is the sample"
words_list = sample_text.split()
shortest_length = len(min(words_list, key=len))
shortest_words = []
for word in words_list:
if len(word) == shortest_length:
shortest_words.append(word)
print("Shortest words:", shortest_words)
print("Length:", shortest_length)
Shortest words: ['is'] Length: 2
Key Points
The
key=lenparameter inmin()tells Python to compare words by their lengthList comprehension provides a concise way to filter elements based on conditions
The
split()method without arguments splits on any whitespace (spaces, tabs, newlines)Using
with open()ensures the file is properly closed after reading
Conclusion
This approach efficiently finds all shortest words by first determining the minimum length, then filtering the word list. The combination of min() with key=len and list comprehension makes the solution both readable and performant.
