Reputation: 527
I have a list with n elements but i would like to convert it to a list which contains n list, and every list contains a single element.
a = ['1','2','3','4']
b = [['1'],['2'],['3'],['4']]
How can I make from a to b?
Upvotes: 3
Views: 753
Reputation: 702
Very Simple Solution Could be from generators
a = ['1','2','3','4']
b = [[item] for item in a]
Upvotes: 0
Reputation: 15433
You can use map
:
b = list(map(lambda x: [x], a))
or a list comprehension:
b = [[i] for i in a]
Upvotes: 3
Reputation: 1032
You can iterate through the a
list and append new list with the item to b
list.
a = ['1','2','3','4']
b = []
for i in range(len(a)):
b.append([a[i]])
print(b)
This is basically the same solution as the one from JRodDynamite, but more readable for beginners.
Upvotes: 0