Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:30:41+00:00 2026-06-04T17:30:41+00:00

Problem: split a string into a list of words by a delimiter characters passed

  • 0

Problem: split a string into a list of words by a delimiter characters passed in as a list.

String: "After the flood ... all the colors came out."

Desired output: ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']

I have written the following function – note I am aware that there are better ways to split a string using some of pythons built in functions but for sake of learning I thought I would proceed this way:

def split_string(source,splitlist):
    result = []
    for e in source:
           if e in splitlist:
                end = source.find(e)
                result.append(source[0:end])
                tmp = source[end+1:]
                for f in tmp:
                    if f not in splitlist:
                        start = tmp.find(f)
                        break
                source = tmp[start:]
    return result

out = split_string("After  the flood   ...  all the colors came out.", " .")

print out

['After', 'the', 'flood', 'all', 'the', 'colors', 'came out', '', '', '', '', '', '', '', '', '']

I can’t figure out why “came out” is not split into “came” and “out” as two separate words. Its like as if the whitespace character between the two words is being ignored. I think the remainder of the output is junk that stems from the problem associated with the “came out” problem.

EDIT:

I followed @Ivc’s suggestion and came up with the following code:

def split_string(source,splitlist):
    result = []
    lasti = -1
    for i, e in enumerate(source):
        if e in splitlist:
            tmp = source[lasti+1:i]
            if tmp not in splitlist:
                result.append(tmp)
            lasti = i
        if e not in splitlist and i == len(source) - 1:
            tmp = source[lasti+1:i+1]
            result.append(tmp)
    return result

out = split_string("This is a test-of the,string separation-code!"," ,!-")
print out
#>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code']

out = split_string("After  the flood   ...  all the colors came out.", " .")
print out
#>>> ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']

out = split_string("First Name,Last Name,Street Address,City,State,Zip Code",",")
print out
#>>>['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip Code']

out = split_string(" After  the flood   ...  all the colors came out...............", " ."
print out
#>>>['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:30:43+00:00Added an answer on June 4, 2026 at 5:30 pm

    You seem to be expecting:

    source = tmp[start:]
    

    To modify the source that the outer for loop is iterating over. It won’t – that loop will keep going over the string you gave it, not whatever object is now using that name. This can mean that the character you’re up to mightn’t be in what’s left of source.

    Instead of trying to do that, keep track of the current index in the string this way:

    for i, e in enumerate(source):
       ...
    

    and what you’re appending will always be source[lasti+1:i], and you just need to keep track of lasti.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really simple problem: I want to split a connection string into its keyword /
The problem is to split a list of items into a variable number of
I am having problem to split string in java. it gives java.util.regex.Pattern.error . String
I have a table that is split into 3. My problem is I have
With Bison, I figured out how to get everything into one long string as
I have a text and split it into words separated by white spaces. I'm
The problem is to write an algorithm to split a given linkedlist efficiently into
I'm trying to split the following string Name=='mynme' && CurrentTime<'2012-04-20 19:45:45' into this: Name
Problem: I am having trouble implementing a recursive image lazy load in all relevant
I am solving the following problem: Suppose I have a list of software packages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.