Reputation: 101
I have a list of lists:
a = [['0.06'], ['0.54'], ['0.61']]
I want to convert this list into normal list like this:
a = [0.06, 0.54, 0.61]
How can I do it it
Upvotes: 3
Views: 6687
Reputation: 2312
You can do it with zip() function (returns tuple):
list(zip(*a))[0]
Output:
('0.06', '0.54', '0.61')
To get list instead of tuple (when you want to modify the list):
list(list(zip(*a))[0])
Output:
['0.06', '0.54', '0.61']
Upvotes: 2
Reputation: 44525
Alternatively, using a functional approach:
list(map(lambda x: float(x[0]), a))
# [0.06, 0.54, 0.61]
Using itertools
to first merge the sublists:
import itertools as it
list(map(float, it.chain.from_iterable(a)))
# [0.06, 0.54, 0.61]
A list comprehension is arguably more elegant and clever.
Upvotes: 1
Reputation: 362796
Using a list comprehension:
>>> a = [['0.06'], ['0.54'], ['0.61']]
>>> [float(x) for [x] in a]
[0.06, 0.54, 0.61]
Upvotes: 3