In python,how to read a file line-by-line into a list?
Answer / Shamendra
To read a file line by line and store each line in a list, you can use a `for` loop or a `list comprehension`. Here's an example using both methods:n```pythonn# Using for loopnmy_list = []nwith open('myfile.txt', 'r') as f:n for line in f:n my_list.append(line) # Be aware that lines will be strings.nn# Using list comprehensionnmy_list = [line for line in open('myfile.txt')]```
| Is This Answer Correct ? | 0 Yes | 0 No |
Does python support object oriented scripting?
Difference between for loop and while loop in Python
What is sorted in python?
what is random module will do in python and what are the functions we can apply on random module
Why is python used?
What does a continue statement do in python?
What is self in python constructor?
How do you create a database in python?
Can we learn python in a week?
What is the use of globals() function in python?
What will be the output of data[-2] from the list data = [1,5,8,6,9,3,4]?
What is abstract class in python?