Reputation: 357
I have a list of lists like the following one:
list = [[226], [44], [8]]
I'd like to convert this list of lists into a list of lists where the numbers are separated by commas. Something like this:
[[2,2,6], [4,4], [8]]
I have been trying to do this using the split
function. I didn't find a duplicate for this question.
Upvotes: 4
Views: 519
Reputation: 388
You can do something like this. This will work even if you have multiple elements in each sublist
like
[[226,542], [44], [8]]
def list_of_list(list):
result = []
for l in list:
for item in l:
sub_list= []
for c in str(item):
sub_list.append(int(c))
result.append(sub_list)
return result
print(list_of_list([[226], [44], [8]]))
Output:
[[2, 2, 6], [4, 4], [8]]
Upvotes: 0
Reputation: 181
This will work:
foo = [[226], [44], [8]]
foo = [eval('[' + ','.join(str(i[0])) + ']') for i in foo]
Or this will work:
foo = [[226], [44], [8]]
foo = [[int(n) for n in str(i[0])] for i in foo]
Upvotes: 3
Reputation: 46
You can create something simple like this:
foo = [ 'a', 'b', 'c' ]
print ",".join(foo)
a,b,c
Hope this helps
Upvotes: 0