1

Suppose, I have a string name = 'baceb'
I want to store its substrings into a list.
For baceb the substrings will be like - "b, ba, bac, bace, a, ac, ace, aceb, c, ce, ceb, e, eb, baceb"
How can I get this substrings easily?

0

2 Answers 2

3

You can generate a list of all the substrings with a simple nested list comprehension:

s = 'baceb'
subs = [s[i:j+1] for i in range(len(s)) for j in range(i,len(s))]

This will however give you a repeated value, b, which occurs as both the first and last substring:

['b', 'ba', 'bac', 'bace', 'baceb', 'a', 'ac', 'ace', 'aceb', 'c', 'ce', 'ceb', 'e', 'eb', 'b']

If you don't want any duplicates, and you don't care about ordering, use a set comprehension instead and then convert to a list:

subs = list({ s[i:j+1] for i in range(len(s)) for j in range(i,len(s)) })

Output:

['e', 'ba', 'a', 'aceb', 'c', 'ceb', 'eb', 'bac', 'baceb', 'bace', 'ce', 'ac', 'b', 'ace']

If you do care about ordering, there are many good solutions here that describe how to remove duplicates from a list while preserving ordering.

Sign up to request clarification or add additional context in comments.

2 Comments

Might be slightly neater to use a set comprehension ({... for i in ...}) rather than list comprehension and then convert to set.
@Oli good point, I had written the set code as a follow-on to the list code, but it's more optimal to do it that way; I've updated the answer.
0

Try this:

import itertools

s = 'baceb'
lst = [[s[i:j+1] for j in range(i,len(s))] for i in range(len(s))]
ss = set(itertools.chain.from_iterable(lst))
print(ss)

Output

{'e', 'ba', 'a', 'aceb', 'b', 'ace', 'bac', 'bace', 'ce', 'c', 'baceb', 'ceb', 'eb', 'ac'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.