0
s="abcabcabc"
S=list(s)
li=[]
l=[]
for i in S:
    if i not in l:
       l.append(i)
    else:
        li.append(l)
        l=[]
        l.append(i)
print(li)

output is

[['a', 'b', 'c'], ['a', 'b', 'c']]

getting only two substring instead of three

the output i want is

[['a','b','c'],['a','b','c'],['a','b','c']]

1 Answers1

0

After the loop end, you also have to check if l is empty or not. If it is non-empty then you have to append that in the final answer.

So, Your code after loop should be,

if len(l) > 0:
    li.append(l)

print(li)
starboy_jb
  • 899
  • 1
  • 6
  • 13