Python Create List From File – 5 Easy Ways

5/5 - (1 vote)

💡 Problem Formulation: We’re often tasked with extracting data from files to conduct manipulation or analysis. For Python developers, a common challenge is to turn contents of a file into a list where each line or element represents an entry.

For instance, given a file named data.txt containing lines of text, we aim to convert each line into an element within a Python list, yielding an output like ['line 1 text', 'line 2 text', ...].

Method 1: Using a for-loop with file object

Creating a list from a file using a for-loop is a straightforward method that grants explicit control over the reading process and can include additional line-processing logic if needed. You open the file, iterate over its lines, and append each line to a list after stripping any newline characters.

Code.py script reading its own content.

Here’s an example:

file_list = []
with open('data.txt', 'r') as file:
    for line in file:
        file_list.append(line.strip())

print(file_list)

This code opens data.txt in read mode, iterates over every line in the file, strips the trailing newline character, and appends the processed line to file_list. It’s simple and clear for small files or when additional processing of each line is needed.

Method 2: Using readlines() method

The readlines() function is a convenient method to read all lines in a file and return them as a list. Each element in the list corresponds to a line in the file.

Here’s an example:

with open('data.txt', 'r') as file:
    file_list = [line.strip() for line in file.readlines()]

print(file_list)

By opening data.txt and calling the readlines() function, we get a list of lines including a newline character at the end of each line. A list comprehension is used to strip these characters, and the result is a clean list of file contents.

Method 3: Using read().splitlines()

The read().splitlines() method is a succinct way to create a list from a file. The read() function reads the entire file into a single string, and splitlines() breaks the string into a list on line boundaries.

Here’s an example:

with open('data.txt', 'r') as file:
    file_list = file.read().splitlines()

print(file_list)

This code snippet reads the entire content of data.txt into a single string and then splits this string into a list of lines using splitlines(). This is a very elegant and Pythonic solution for creating a list from file lines.

Method 4: Using file object as an iterator

In Python, file objects can be used directly as iterators, where each iteration yields a line from the file. This method is memory efficient and suitable for large files.

Here’s an example:

with open('data.txt', 'r') as file:
    file_list = list(line.strip() for line in file)

print(file_list)

The file iterator is wrapped with a generator expression that strips each line. Then the list() constructor turns it into a list. This method combines the efficiency of an iterator with the simplicity of list comprehension.

Bonus One-Liner Method 5: Using map() function

The map() function is perfect for applying a transformation—like stripping newline characters—to each element of an iterable, such as the lines of a file.

Here’s an example:

with open('data.txt', 'r') as file:
    file_list = list(map(str.strip, file))

print(file_list)

The map() function applies the str.strip method to every line read from the file, removing any extra whitespace, including line breaks. The result is then converted to a list with list().

Summary/Discussion

  • Method 1: Direct control over line processing with additional logic possible.
  • Method 2: Simple and pythonic, but reads all lines into memory at once.
  • Method 3: Extremely concise, reads the whole file into memory, useful for smaller files.
  • Method 4: Memory-efficient, best suited for very large files.
  • Method 5: A functional programming approach, concise and elegant.

In summary, for small files or when line-by-line processing is needed, methods 1 or 4 are adequate. If we prioritize brevity, methods 3 or 5 are preferable. For medium-sized files with less complexity, method 2 is a great balance between simplicity and functionality.