Chris McDonough wrote:

>> Can Python
>> create list dynamically, I want to implement a program which will read
>> data from a file and store each line into a list, is this possible?
> 
> L = []
> [L.append(line) for line in (open('filename.txt')]

Why would you create two lists, one filled only with None entries just to
throw it away immediately? Don't use list comprehensions just because you
can. 

Here are two sane approaches:

lines = open(filename).readlines()
lines = list(open(filename)) # a bit more generic

Finally, if you want to strip off the trailing newlines, a list
comprehension is in order:

lines = [line[:-1] for line in open(filename, "U")]

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to