Lichao Zhang Lichao Zhang - 2 months ago 6
Python Question

Delete the first elements of each row in list in python2.7

My list looks like:
['0 0.690001', '1 0.970671', '2 1.520989', '3 1.946516', '4 2.229378']

how can I get
[ 0.69000,0.970671,1.520989,1.946516,2.229378]

Answer

Use list comprehension as:

my_list = ['0 0.690001', '1 0.970671', '2 1.520989', '3 1.946516', '4 2.229378']
[float(item.split()[1]) for item in my_list]

OR, you may also use map():

map(lambda x: float(x.split()[1]), my_list)
Comments