Producing Nested Lists for each Value in a List

Viewed 42

How is it possible to make each value in a list into its own list?

Say we're given:

Numbers = [1, 2, 3, 4, 5]

I would suspect that a for or while loop would do the trick:

for number in Numbers:
    Numbers.append([number])

but this seems to not satisfy what I'd like to do.

1 Answers

Try using a list comprehension:

print([[i] for i in Numbers])

Or use map:

print(list(map(lambda x: [x], Numbers)))

Output:

[[1], [2], [3], [4], [5]]
Related