Program to Read File into List in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

We can read a file using Python's built-in file.read() method. After that, we can use different functions to read the data obtained from the file into a list. In this article, we will see the program to read a file into a list in Python.

How to Read File into List in Python?

In Python programming language, we use the file.read() function to read the data of a file. This function returns the data of the file as a string. We can store this data returned in string format and manipulate it using functions (we'll discuss them in detail further in this article) to convert it into a list.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Using read() and split() Functions

As we have discussed before, we can use the file.read() function to read the data of the file, and it'll return the data in the string format. Now, we can use the split() method on this string data.

The split() method takes one mandatory parameter, a separator. The split() method returns the data in a list by breaking the string according to the specified separator. Now, let's understand this better through an example.

File

File

Code

Output

Explanation First, we open the file in the read mode using the open(); then we use the split function. Inside the split function, we have used the comma(,) as the separator and passed it into the split() method argument. This will create a list from the file's data. In the list, the string will keep going at the same index until a comma is encountered; after that, it'll move to the next index. The function will stop reading once it reaches the end of the string.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Using readlines() Function

The readlines() function is an in-built Python method that can be used to read the file into a list. The readlines() function returns a list where each list element is a line in the file.

File

Readlines Function File

Code

Output

Explanation The way we read files will remain the same in every method, so in this. The difference in this method from the previous one is the use of the readlines() function instead of read() and split(). The readlines() function returns a list in which every list element is a line from the file. We save this list returned by the function in a variable and print it at the end.

Using loadtxt() Function

Until now, all the methods we discussed before used built-in Python functions like read() and split(), and readlines(). However, in this method, we'll be using the loadtxt() function, a module of the Numpy library.

Note: This method can only be used to read numbers from a file and not a string.

File

Loadtxt Function File

Code

Output

Explanation The first thing we need to do here is import the loadtxt() function from the numpy library. After this, we can use the loadtxt() function to read the file data into a list. The loadtxt() function uses a delimiter as its second parameter to know where to break the file data and create one list element from it. In this example, we are using the period (".") as the delimiter. Hence, the loadtxt() function will convert the file data into a list, where a list element will be created according to the delimiter used.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Read More

Conclusion

That's all for this article. We have covered all the significant ways to read a file into a list in Python.

  • We discussed different programs to read a file into a list in Python using three main methods:

    • read() and split()
    • readlines()
    • loadtxt()
  • To learn more about the topic of file handling and different ways to read a file in Python, check out the read more section.