Georgy Georgy - 3 months ago 8
Python Question

How to convert list of ints into list of lists?

The problem is: how to convert properly (in "pythonic way") list of ints:

[1, 3, 7, 6]
into corresponding list of lists:
[[1], [3], [7], [6]]
with ints inside of them?

Tried to do it like:
unsorted_list = [list(str(x)) for x in unsorted_list]
but get
[['1'], ['3'], ['7'], ['6']]
. Don't know how to perform the same on ints, they are not iterable. Using
for
afterwards can solve the problem, but not so elegant.
Thank you.

Answer

You shouldn't transform the integers to strings. Simply:

[[x] for x in unsorted_list]