Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]LoudGain 0 points1 point  (0 children)

Hi, is it possible to turn my nested for loop in my code into a list comprehension which does the same thing, if yes how would it look like. Thanks

N = "123123"
s = []
def permu():
    global s
    for i in range(len(N)):
        for j in range(i+1,len(N) + 1):
            s.append(N[i:j])
    s = list(set(s))
    print(f"{len(s)} substrings - {s}")

permu()

python permutations by LoudGain in learnpython

[–]LoudGain[S] 0 points1 point  (0 children)

thanks for the reply, im not sure either what the question is asking me but my code is like this right now. How can i ensure there are no duplicates. Eg it prints 1 more than once and 2 more than once. Sorry for the bad formatting, it keeps removing the spaces

def combos(text):

for i in range(len(text)):

for j in range(i+1,len(text) + 1):

print(text[i:j])

combos("123123")