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.

</>
Copy
# 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:

  1. open("data.txt", "w") – Creates a file and writes a space-separated list of fruits.
  2. open("data.txt", "r") – Reads the content of the file.
  3. file.read() – Reads the entire file as a single string.
  4. 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.

</>
Copy
# 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:

  1. file.write() – Writes numbers into a file, each on a new line.
  2. file.readlines() – Reads all lines from the file and returns a list.
  3. 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.

</>
Copy
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:

  1. json.dump() – Saves the list to a file in JSON format.
  2. 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.

</>
Copy
# 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:

  1. file.write() – Writes the list as a string to the file.
  2. file.read() – Reads the entire content as a string.
  3. 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:

  1. split(): Reads a space-separated list.
  2. readlines(): Reads each line as an element.
  3. json module: Reads structured JSON data.
  4. eval(): Converts a string representation of a list.