arraysnon-repetitive

how to get a list of non repeating substring from a string?


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

Solution

  • 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)