Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list to list of lists

Tags:

python

list

Is there an easy way to convert a list of doubles into a list of lists of doubles?

For example:

[1.0, 2.0, 3.0]

into

[[1.0], [2.0], [3.0]]

The code I'm using requires the second as inputs to a bunch of functions but it's annoying to have two copies of the same data.

like image 213
ad_ad Avatar asked Dec 18 '25 18:12

ad_ad


1 Answers

Just use a list-comprehension wrapping each element in a list:

l = [1.0, 2.0, 3.0]
print [[x] for x in l]
[[1.0], [2.0], [3.0]]
like image 150
Padraic Cunningham Avatar answered Dec 20 '25 12:12

Padraic Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!