Problem Formulation: Consider that you are trying to read the contents of a text file. How will you read the text from the file and store it in a Python list?
To gain better clarity over the problem, let’s have a look at a similar question asked on StackOverflow:
Before we dive into the solution, let’s answer the first question –
Question: “Why is the entire file content being stored as a single item within the list?”
Answer: Simply put, you are reading the entire content of the file at once and then storing it as a single item within the list. Python has no clue where to split the content and store it as an individual element within the list. In other words, the developer has not given Python a way to determine how to separate individual texts from the file and then store them as separate items within the list.
With the first doubt out of our way, let us dive into the mission-critical question – “How to store the contents of a text file as separate elements in a Python list?”
Discussion: The contents of a file contain a sequence of characters. When you split the contents of the file, they are treated as separate list of values based on a specified delimiter. For example, a text file with the content “0,0,200,0,53,1,0,255” can be split on the delimiter “,” and it will create the list
['0' ,'0' ,'200' , '0', '53', '1', '0', '255'].
You can download the file being used as an example in this tutorial here:
Well! Let’s dive into the numerous ways of achieving the desired output.
Solution: Using str.split()
Approach: Read the entire content of the file as a string using the
file.read() function. Use the
str.split(separator) function with this string to return a list of values containing each text of the file as a separate item. The separator to be used in the given case is “,”.
file = open("demo.txt", "r") contents = file.read() list_of_contents = contents.split(",") file.close() print(list_of_contents)
['0', '0', '200', '0', '53', '1', '0', '255']
💡A Quick Recap to split() Function:
split() is a built-in method in Python that is used to cut/split a given string based on a given separator. You can specify any separator according to your requirement. However, by default, the separator is a whitespace.
- separator is an optional parameter that is used to specify the separator (delimiters). By default, it is any whitespace character.
- maxsplit is an optional parameter that allows us to specify the maximum number of splits that we want to perform. By default its value is -1 which is “all occurrences”.
Related Read: How To Cut A String In Python?
Scenario 2: Separate Contents From Each Line of Text File and Store as a List
In the previous case, the file had all the contents stored within a single line of code. But what if you have a file that not only has contents separated by a delimiter but also has numerous lines of texts with comma-separated text?
Example: Consider the file given below:
[‘0’, ‘0’, ‘200’, ‘0’, ’53’, ‘1’, ‘0’, ‘255’, ‘1.5’, ‘2.5’, ‘3.5’, ‘4.5’, ‘5.5’, ‘0.1’, ‘0.2’, ‘0.3’, ‘0.4’, ‘0.5’]
Solution: Separately Store Each Line in a List and Flatten the List
Since, you not only have to take care of the delimiter but also consider each new line; you can follow a two-step process in this case.
- Step 1: Separate each line and store them as individual elements in a list. At this point you will have a list containing each line stored as an item within the list. According to the given question, the list will look something like this:
['0,0,200,0,53,1,0,255', '1.5,2.5,3.5,4.5,5.5', '0.1,0.2,0.3,0.4,0.5']
- Step 2: Create independent lists within the above list by separating each item on the basis of the delimiter. So the list now represents a list of lists:
[['0,0,200,0,53,1,0,255'], ['1.5,2.5,3.5,4.5,5.5'], ['0.1,0.2,0.3,0.4,0.5']]
- Step 3: Finally, flatten the list formed in step 2. Note that step 2 and step 3 can be merged into a single step. To learn how to flatten a list of lists you can refer to this tutorial: Flatten A List Of Lists In Python.
separated_lines = [line.strip() for line in open('demo.txt')] flattened_list = [item for i in separated_lines for item in i.split(",")] print(flattened_list) # ['0', '0', '200', '0', '53', '1', '0', '255', '1.5', '2.5', '3.5', '4.5', '5.5', '0.1', '0.2', '0.3', '0.4', '0.5']
However, if you just want to store an entire line of a given file as an item in a list then you can refer this comprehensive guide: How to Read a File Line-By-Line and Store Into a List?
With that, we come to the end of this article, and we have gone through different scenarios that demonstrate how you can store the contents of a text file into a list in Python. Please subscribe and stay tuned for more interesting discussions and tutorials.
Happy coding! 🙂
For any software developer it is crucial to master the IDE well, to write, test and debug high-quality code with little effort. Do you want to master all these in the most popular Python IDE fast?
This course will take you from beginner to expert in PyCharm in ~90 minutes.
Join the PyCharm Masterclass now, and master PyCharm by tomorrow!