Read a List from a File in Python
To read a list from a file in Python, we typically read the file’s contents, process the data, and convert it into a list. This can be achieved using methods like read()
, readlines()
, or eval()
. In this tutorial, we will explore multiple ways to read a list from a file with practical examples.
Examples
1. Reading a List from a File Using read()
and split()
We can read a list of values from a file and use the split()
method to convert the string into a list.
# Creating a sample file
with open("data.txt", "w") as file:
file.write("apple banana cherry orange")
# Reading the list from the file
with open("data.txt", "r") as file:
content = file.read()
my_list = content.split()
# Printing the list
print("List from file:", my_list)
Explanation:
open("data.txt", "w")
– Creates a file and writes a space-separated list of fruits.open("data.txt", "r")
– Reads the content of the file.file.read()
– Reads the entire file as a single string.content.split()
– Splits the string into a list using whitespace as the delimiter.
Output:
List from file: ['apple', 'banana', 'cherry', 'orange']
2. Reading a List from a File Using readlines()
The readlines()
method reads all lines from a file and returns a list, where each line is a separate element.
# Creating a sample file
with open("numbers.txt", "w") as file:
file.write("10\n20\n30\n40\n50")
# Reading the list from the file
with open("numbers.txt", "r") as file:
my_list = file.readlines()
# Stripping newline characters
my_list = [line.strip() for line in my_list]
# Printing the list
print("List from file:", my_list)
Explanation:
file.write()
– Writes numbers into a file, each on a new line.file.readlines()
– Reads all lines from the file and returns a list.line.strip()
– Removes newline characters from each element.
Output:
List from file: ['10', '20', '30', '40', '50']
3. Reading a List from a File Using json
Module
When working with structured data, we can store and retrieve lists using the json
module.
import json
# Creating a sample file
with open("data.json", "w") as file:
json.dump(["red", "green", "blue", "yellow"], file)
# Reading the list from the file
with open("data.json", "r") as file:
my_list = json.load(file)
# Printing the list
print("List from file:", my_list)
Explanation:
json.dump()
– Saves the list to a file in JSON format.json.load()
– Reads the JSON file and converts it back into a Python list.
Output:
List from file: ['red', 'green', 'blue', 'yellow']
4. Reading a List from a File Using eval()
The eval()
function can be used to interpret a string representation of a list from a file.
# Creating a sample file
with open("list_data.txt", "w") as file:
file.write("['Python', 'Java', 'C++', 'Ruby']")
# Reading the list from the file
with open("list_data.txt", "r") as file:
content = file.read()
my_list = eval(content)
# Printing the list
print("List from file:", my_list)
Explanation:
file.write()
– Writes the list as a string to the file.file.read()
– Reads the entire content as a string.eval(content)
– Converts the string back into a list.
Output:
List from file: ['Python', 'Java', 'C++', 'Ruby']
Conclusion
Python provides multiple ways to read a list from a file:
split()
: Reads a space-separated list.readlines()
: Reads each line as an element.json
module: Reads structured JSON data.eval()
: Converts a string representation of a list.