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?

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Shakiib
  • 57
  • 1
  • 6

2 Answers2

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.

Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    Might be slightly neater to use a set comprehension (`{... for i in ...}`) rather than list comprehension and then convert to set. – Oli Sep 07 '20 at 00:43
  • @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. – Nick Sep 07 '20 at 01:19
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'}
Mike67
  • 11,175
  • 2
  • 7
  • 15