file = [float(line.partition(' ')[0]) for line in file]
the file is object of open file...
thnq
file = [float(line.partition(' ')[0]) for line in file]
the file is object of open file...
thnq
For measurement of speed, see the Python standard library module timeit:
$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.123 usec per loop
$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.split(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.132 usec per loop
$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.split(" ", 1)[0]) for line in f]'
10000000 loops, best of 3: 0.127 usec per loop
partition seems to be faster than split, at least. I can't think of a faster way right now, so well done.
It depends what you are going to do with the list once it has been created. If you are just going to iterate through it then it may be better to use a generator expression so that you do not load the entire file into memory at once. If the file is large this could result in page swapping or out of memory errors.
I have read through the related questions you posted, and can see no information on what problem you are trying to solve or what you are going to do with the data once you have read it. If you give us some more context then we may be able to give more specific and helpful answers.
If you're only interested in everything that comes before the first space (and assuming there always is one), you can try using string index:
[float(line[:line.index(" ")]) for line in f]
Borrowing Lars's tests, it runs faster than partition:
rbp@apfelstrudel ~$ python -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.192 usec per loop
rbp@apfelstrudel ~$ python -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line[:line.index(" ")]) for line in f]'
10000000 loops, best of 3: 0.181 usec per loop
Also, of course, if you exchange the outer square brackets for parenthesis, you'll get a generator expression, which doesn't generate all the results straight away. Depending on how you'll use this, it may fit into the "smarter" category :)
Edited to add:
... Although, since SilentGhost mentioned this is py3k, the speed difference is not relevant then:
rbp@apfelstrudel ~$ python3 -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line[:line.index(" ")]) for line in f]'
100000 loops, best of 3: 10.9 usec per loop
rbp@apfelstrudel ~$ python3 -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
100000 loops, best of 3: 11 usec per loop
But I still think index is better, as it clearly shows what you mean (as opposed to partition, which gives you two additional values that you throw away immediately)