Georgy Georgy - 1 year ago 80
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
afterwards can solve the problem, but not so elegant.
Thank you.

Answer Source

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

[[x] for x in unsorted_list]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download