I'm trying to get Python to a read line from a .txt file and write the elements of the first line into a list. The elements in the file were tab- separated so I used split("\t")
to separate the elements. Because the .txt file has a lot of elements I saved the data found in each line into a separate list.
The problem I currently have is that it's showing each list like this:
['Name1', '7.3', '6.9', '6.6', '6.6', '6.1', '6.4', '7.3\n']
How can I remove \n
from the last element of the list and make it just '7.3'
?
From Python3 onwards
map
no longer returns alist
but amapObject
, thus the answer will look something likeYou can read more about it on What’s New In Python 3.0.
So now what are the ways of getting trough this?
Case 1 - The
list
call overmap
with alambda
map
returns an iterator.list
is a function that can convert an iterator to a list. Hence you will need to wrap alist
call aroundmap
. So the answer now becomes,Very good, we get the output. Now we check the amount of time it takes for this piece of code to execute.
2.22 microseconds. That is not so bad. But are there more efficient ways?
Case 2 - The
list
call overmap
withOUT alambda
lambda
is frowned upon by many in the Python community (including Guido). Apart from that it will greatly reduce the speed of the program. Hence we need to avoid that as much as possible. The toplevel functionstr.strip
. Comes to our aid here.The
map
can be re-written without usinglambda
usingstr.strip
asAnd now for the times.
Fantastic. You can see the efficiency differences between the two ways. It is nearly 60% faster. Thus the approach without using a
lambda
is a better choice here.Case 3 - Following Guidelines, The Regular way
Another important point from What’s New In Python 3.0 is that it advices us to avoid
map
where possible.So we can solve this problem without a
map
by using a regularfor
loop.The trivial way of solving (the brute-force) would be:-
The timing setup
And the result.
As you can see the brute-force is a bit slower here. But it is definitely more readable to a common programmer than a
map
clause.Case 4 - List Comprehensions
A list comprehension here is also possible and is the same as in Python2.
Now for the timings:
As you can see the list-comprehension is more effective than
map
(even that without alambda
). Hence the thumb rule in Python3 is to use a list comprehension instead ofmap
Case 5 - In-Place mechanisms and Space Efficiency (T-M-T)
A final way is to make the changes in-place within the list itself. This will save a lot of memory space. This can be done using
enumerate
.The timing result would be
1.4806894720022683
. But however this way is space effective.Conclusion
A comparitive list of timings (Both Python 3.4.3 and Python 3.5.0)
Finally note that the list-comprehension is the best way and the
map
usinglambda
is the worst. But again --- ONLY IN PYTHON3If you want to remove
\n
from the last element only, use this:If you want to remove
\n
from all the elements, use this:You might also consider removing
\n
before splitting the line:from this link:
you can use rstrip() method. Example
Since the OP's question is about stripping the newline character from the last element, I would reset it with
the_list[-1].rstrip()
:It's O(1).
Using list comprehension:
You could do -
The
lines
has got all the contents of your file.One could also use list comprehensions to make this more compact.