Bret
Bret

Reputation: 81

Producing Nested Lists for each Value in a List

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.

Upvotes: 2

Views: 44

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

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]]

Upvotes: 1

Related Questions