I want to generate all substrings of size k-1 from a string of size k.
e.g 'abcd' should give me
Order of these strings in the list doesnt matter.
Also order doesnt matter inside the string e.g 'abc' or 'bca' or 'bac' is the same.
The following code doesnt give the full output(iteration over each string)

subsetList = []
prunedNew = ['abcd']
for element in prunedNew:
    for i in range(0,2):
        subsetList.append(element[i:i+len(element)-1])
        continue
    continue
 return prunedNew

Thanks,
girish

Dani AI

Generated

Two interpretations are worth stating up front so the result matches the intended task.

  • If the goal is "remove exactly one character from the original string and return the k results", then for a length-k string there are exactly k outputs: remove the character at each index. Example: from "abcd" the set is {"bcd", "acd", "abd", "abc"} (order of the list doesn't matter).
  • If instead the order of characters inside each result is irrelevant (you care about the combination of characters, not their order), canonicalize each substring (for example by sorting) and deduplicate — useful when the original contains repeated letters.

A concise, robust approach using the standard library (avoids manual index arithmetic mistakes) is to use itertools.combinations to pick k-1 characters and join them:

from itertools import combinations

s = "abcd"
subs = [''.join(chars) for chars in combinations(s, len(s) - 1)]
# subs -> ['abc', 'abd', 'acd', 'bcd']

If duplicates must be removed but insertion order preserved:

unique_subs = list(dict.fromkeys(subs))

If different permutations should be treated as the same (order-insensitive):

canonical = {''.join(sorted(chars)) for chars in subs}

Troubleshooting notes (addresses the original attempts): iterate over every index (range(len(s))) instead of a hardcoded small range; when slicing, skip the single character at index i rather than using a fixed-length slice window. Expect O(k^2) work (k slices of length ~k), which is trivial for small k but matters only for very large strings. Pay attention to repeated characters — they create duplicate results unless explicitly deduplicated.

's removal idea is correct in intent; the itertools approach shown here is a compact, readable alternative and the canonicalization/dedup steps cover the "order doesn't matter inside the string" requirement mentioned by . 's question about use cases is reasonable — this pattern commonly appears in k-mer enumeration, sliding-window processing, and neighbor-generation for simple graph constructions.

Recommended Answers

All 3 Replies

Could you enlighten me, what is this all used for?

actually i'm implementing an algorithm in PYthon...right now there are parts of it that i need to rectify...

i think the following code may solve your problem, it was thought up by a friend of mine, i merely did the typing, so i take no credit, but i think this is your solution:

def main():
    string = "abcd"
    k = len(string)
    a = 0
    SubStrings = []
    while k > 0:
        SubStrings.append(string[0:a]+string[a+1:])
        k -= 1
        a += 1
    return SubStrings
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.