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 Split a File into a List in Python?
Python provides multiple ways to read a file and split its contents into a list. This is a common operation in data processing, text analysis, and file manipulation tasks.
Understanding Files and Lists in Python
In Python, a file is a named location on disk where data is stored permanently. Python can work with various file formats including text files (.txt), CSV files (.csv), and others.
A list is a built-in data structure that can store multiple items in a single variable. Lists are mutable, meaning you can modify them after creation.
Method 1: Using read() and split()
The most basic approach involves reading the entire file content and then splitting it into a list ?
# Create a sample file first
with open("sample.txt", "w") as file:
file.write("Line 1\nLine 2\nLine 3\nLine 4")
# Read and split the file
with open("sample.txt", "r") as file:
content = file.read()
lines = content.split("\n")
print(lines)
['Line 1', 'Line 2', 'Line 3', 'Line 4']
Method 2: Using readlines()
Python's readlines() method reads all lines and returns them as a list of strings ?
# Create a sample file
with open("sample.txt", "w") as file:
file.write("Apple\nBanana\nCherry\nDate")
# Read lines into a list
with open("sample.txt", "r") as file:
lines = file.readlines()
print(lines)
print("After removing newlines:", [line.strip() for line in lines])
['Apple\n', 'Banana\n', 'Cherry\n', 'Date'] After removing newlines: ['Apple', 'Banana', 'Cherry', 'Date']
Method 3: Using List Comprehension
You can create a clean list without newline characters using list comprehension ?
# Create a sample file
with open("data.txt", "w") as file:
file.write("Python\nJava\nJavaScript\nC++")
# Read and clean lines in one step
with open("data.txt", "r") as file:
languages = [line.strip() for line in file]
print(languages)
print(f"Total languages: {len(languages)}")
['Python', 'Java', 'JavaScript', 'C++'] Total languages: 4
Splitting by Different Delimiters
You can split file contents using different delimiters like commas or spaces ?
# Create a CSV-like file
with open("numbers.txt", "w") as file:
file.write("1,2,3,4,5\n6,7,8,9,10")
# Split by commas
with open("numbers.txt", "r") as file:
content = file.read()
numbers = content.replace('\n', ',').split(',')
print(numbers)
print("As integers:", [int(num) for num in numbers])
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] As integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Comparison
| Method | Memory Usage | Best For | Newlines Included |
|---|---|---|---|
read().split() |
High | Small files, custom delimiters | No |
readlines() |
High | Line-by-line processing | Yes |
| List comprehension | Low | Large files, clean output | No (with strip) |
Best Practices
Always use the with statement when working with files. It automatically closes the file and handles exceptions ?
# Good practice - file is automatically closed
try:
with open("example.txt", "r") as file:
data = [line.strip() for line in file]
print(f"Successfully read {len(data)} lines")
except FileNotFoundError:
print("File not found!")
except Exception as e:
print(f"Error: {e}")
File not found!
Conclusion
Use readlines() for simple line-by-line reading, read().split() for custom delimiters, and list comprehension for memory-efficient processing. Always use the with statement for proper file handling.
