Summary: Use one of the following ways to read a file line by line and store into a list:
- Using The
readlines
Andstrip
Method - Using
rstrip()
- Use the
for
Loop andstrip()
method - Use
splitlines()
- Use The
pathlib
Library And Thesplitlines()
Method - Use List Comprehension
Problem: How to read every line of a file in Python and store each line as an element in a list?
In this article we are going to discuss how we can –
- Read a file line by line.
- Then store it in a list.
Let us have a look at an example given below that we will be referring while discussing the solutions.
Given File:
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
In the above example, we have a file by the name test.txt that stores the names of a few well-known personalities ?. Our purpose is to read each line (which in this case contains names) one by one and store them in a list.
Note: The file taken into consideration is the same file as mentioned in the example above. Therefore the solution derived is in accordance with the same file. I have attached the file below ? for your convenience. Please feel free to download it in case you want to practice with it.
Without further delay let us dive into the solutions.
Method 1: Using The readlines And strip Methods
readlines()
is a built-in method in Python used to read a file line by line and then store each line in a list.string.strip()
: Removes leading and trailing whitespaces including newline characters ‘\n’ and tabular characters ‘\t’.
We are going to use the readlines()
method to read the file line by line while the strip()
method is used to get rid of the new line character '\n'
while storing the elements in the list. Let us have a look at the following program to visualize how we can solve our problem using the above-mentioned methods.
with open('test.txt') as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line li = [x.strip() for x in content] print(li)
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
Method 2: Using line.rstrip()
string.rstrip()
is a built-in function in Python that removes all whitespaces on the right of the string (trailing whitespaces). Thus, we can use it to strip or separate elements out of each line and then store them in a list using the [] notation.
Example:
with open('test.txt') as f: lines = [line.rstrip() for line in f] print(lines)
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
Method 3: Using the for Loop and strip() method
Another approach to our problem is to use a for loop to iterate over the lines in the file one by one and then append them to a list using the append()
function. The strip()
function again comes into play which allows us to strip the newline character.
with open("test.txt") as file_in: lines = [] for line in file_in: lines.append(line.strip('\n')) print(lines)
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
Method 4: Using splitlines()
❖ splitlines()
is an inbuilt function in Python which is used to split a string breaking at line boundaries.
Example:
# Open the file for reading. with open('test.txt', 'r') as infile: data = infile.read() # Read the contents of the file into memory. # Return a list of the lines, breaking at line boundaries. li = data.splitlines() print(li)
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
In the solution above, we’re opening the file for reading and assigning it to the variable ‘infile
.’ Once the code has finished running, the file will be automatically closed. Then we use the splitlines()
method to store it in a list by storing each line of the file as a separate element.
Method 5: Using The pathlib Library And The splitlines() Method
The pathlib library was introduced in Python 3.4 and has a handy method known as read_text()
which is a nice way to read the file without having to worry about opening or closing it. The splitlines
function turns the contents of the file into a list containing the elements of the file line by line.
Example:
from pathlib import Path p = Path('test.txt') lines = p.read_text().splitlines() print(lines)
Output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
Method 6: Using List Comprehension
List comprehension is a compact way of creating lists. The simple formula is [expression + context]
.
- Expression: What to do with each list element?
- Context: What elements to select? The context consists of an arbitrary number of
for
andif
statements.
The example [x for x in range(3)]
creates the list [0, 1, 2]
.
If you want to learn more about list comprehensions, please have a look at our blog tutorial here. Now let us have a look at a one-line solution to our problem using list comprehension.
print([line.rstrip() for line in open('test.txt')])
output:
['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']
Method 7: Reading a csv File Line By Line And Store In a List
Thus far we have seen how we can read a text file line by line and store the elements in a list. Now let us discuss how we can do the same for a csv file. The approach used by us, in this case, is the pandas library in Python which allows us to read the data from the csv file and store the values in an array. We can convert the array to a list using the tolist()
method.
The file that we are going to mention in the example to follow looks like the one given below.
Now let us have a look at the solution to our problem in the program given below.
import pandas as pd data = pd.read_csv('test.csv') # You can also add parameters such as header, sep, etc. array = data.values print(array.tolist())
Output:
[['Bill Gates'], ['Mark Zuckerberg'], ['Bernard Arnault & family'], ['Mukesh Ambani'], ['Steve Ballmer'], ['Warren Buffett'], ['Larry Page'], ['Elon Musk'], ['Sergey Brin']]
Conclusion
I hope that after reading this article you can read files line by line and then store the elements in a list such that each line represents an element of the list. Please subscribe and stay tuned for more interesting articles!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.