0

I am a newbie by python and now am stuggling because can't get the needed output. I also tried many solutions which I found here, but somehow I can't get it correctly. So, please, explain me, what is my mistake.

I have a list like this:

list_of_l= [['ototot'], ['tototo'], ['ttoott'], ['ottott']]

what I wanna have is something like this

new_list= [['o','t','o','t','o','t'], ['t','o','t','o','t','o'], ['t','t','o','o','t','t'], ['o','t','t','o','t','t']]

and I want to have a for loop, because my list_of_l may have a different length.

so what I tried so far:

I get only the wished split when i work with string

t='ototot'
o=[t[i:i + 1] for i in range(0, len(t), 1)]
print(o)
Output: ['o','t','o','t','o','t']

the problem is that when I try to make a for loop, I cant split it, because it is a list and not a str. My question may be kinda stupid, but I really don't get it.

So I tried to make a for loop, but failed...

for k in range (len(list_of_l)):
    # and here is my disaster, I don't get what to do here.

Please help T-T

UPDATE:

It is working now! I am very thankful for your help everyone and sorry, if my question was kinda stupid :D


UPDATE 2:

And then just random question(I want just to understand how it works), is it also possible to make a list of list of lists?? No idea how to explain it, something like this:

list_of_l= [['ototot'], ['tototo'], ['ttoott'], ['ottott']]
new_list= [[['o'],['t'],['o'],['t'],['o'],['t']], .......]
Andy133
  • 1
  • 1
  • you can just map a string to a list: `l = list(map(lambda x: list(x[0]), list_of_l))`. note: have to use `x[0]` here since the strings are list elements themselves. You could also flatten the list of lists upfront. – FObersteiner Dec 16 '19 at 15:27
  • #add: including [list flattening](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists), that would make `out = [list(s) for l in list_of_l for s in l]` - now you can also have multiple strings in a sublist. – FObersteiner Dec 16 '19 at 15:33

4 Answers4

1

Is the second-layer list ['ototot'] guaranteed to have only one string in it, or can it have multiple?

Starting from where you were, you can do almost the same thing, but access the string from within the list:

t = ['ototot']
t_string = t[0]
# Do the same thing that you did up there, to break it up.

What you said before was that you can't do the split properly, because it's a list containing a string. So the straightforward thing then is to get that string, so that you can split it.

itsdchen
  • 11
  • 2
  • Yes, its quaranteed to be only one string. I also thought that it is because I can't use split on a list, but I was searching for a solution how to change it nicely. Thanks for your answer! – Andy133 Dec 16 '19 at 15:58
0

Something like this?

print([[j for i in l for j in i] for l in list_of_l])
Nuno Palma
  • 514
  • 3
  • 9
0

You can do it like this

list_of_l= [['ototot'], ['tototo'], ['ttoott'], ['ottott']]
newList=list()
for i in list_of_l:
    newList.append(list(i[0]))
print(newList)

here is the output

[['o', 't', 'o', 't', 'o', 't'], ['t', 'o', 't', 'o', 't', 'o'], ['t', 't', 'o', 'o', 't', 't'], ['o', 't', 't', 'o', 't', 't']]
M Hamza Razzaq
  • 432
  • 2
  • 7
  • 15
  • Kindly vote up if you find my answer useful. Thanks – M Hamza Razzaq Dec 16 '19 at 16:10
  • I tried to vote, but I get this one "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Andy133 Dec 16 '19 at 16:20
-1

Code:

list_of_l= [['ototot'], ['tototo'], ['ttoott'], ['ottott']]

[list(i[0]) for i in list_of_l]
byxor
  • 5,930
  • 4
  • 27
  • 44