Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


In python,how to read a file line-by-line into a list?



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

Post New Answer

More Python Interview Questions

Does python support object oriented scripting?

1 Answers  


Difference between for loop and while loop in Python

1 Answers  


What is sorted in python?

1 Answers  


what is random module will do in python and what are the functions we can apply on random module

1 Answers  


Why is python used?

1 Answers  


What does a continue statement do in python?

1 Answers  


What is self in python constructor?

1 Answers  


How do you create a database in python?

1 Answers  


Can we learn python in a week?

1 Answers  


What is the use of globals() function in python?

1 Answers  


What will be the output of data[-2] from the list data = [1,5,8,6,9,3,4]?

1 Answers  


What is abstract class in python?

1 Answers  


Categories