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

  • Home
  • SEARCH
  • 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 7659243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:18:26+00:00 2026-05-31T13:18:26+00:00

guys, I’m a programming newbie trying to improve the procedure bellow in a way

  • 0

guys, I’m a programming newbie trying to improve the procedure bellow in a way that when I pass it this argument: split_string("After the flood ... all the colors came out."," .") it returns it:

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

and not this:

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

Any hint of how to do this? (I could just iterate again the list and delete the ” elements, but I wanted a more elegant solution)

This is the procedure:

def split_string(source, separatorList):
    splited = [source]
    for separator in splitlist:
        source = splited
        splited = []
        print 'separator= ', separator
        for sequence in source:
            print 'sequence = ', sequence
            if sequence not in splitlist and sequence != ' ':
                splited = splited + sequence.split(separator)            
    return splited

print split_string("This is a test-of the,string separation-code!", " ,!-")
print
print split_string("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-05-31T13:18:27+00:00Added an answer on May 31, 2026 at 1:18 pm

    Let’s see where did the empty strings come from first, try to execute this in shell:

    >>> 'After the'.split(' ')

    result:

    ['After', '', 'the']

    This was because when split method came to ' ' in the string, it find nothing but '' between two spaces.

    So the solution is simple, just check the boolean value of every item get from .split(

    def split_string(source, separatorList):
        splited = [source]
        for separator in separatorList:
            # if you want to exchange two variables, then write in one line can make the code more clear
            source, splited = splited, []
            for sequence in source:
                # there's no need to check `sequence` in advance, just split it
                # if sequence not in separatorList and sequence != ' ':
                    # splited = splited + sequence.split(separator)
    
                # code to prevent appearance of `''` is here, do a if check in list comprehension.
                # `+=` is equivalent to `= splited +`
                splited += [i for i in sequence.split(separator) if i]
        return splited
    

    More details about [i for i in a_list if i] see PEP 202

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

Sidebar

Related Questions

Guys that is code copied from a book (Programming Windows 5th edition): #include <windows.h>
Guys, this can't be for real I'm trying to make a .NET 2.0 executable
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
Guys, I am using SQL Server 2000 and executing the sp_columns stored procedure to
Guys, what is function called after my class loaded, where i can call self
guys i have arrays in which i have to match this kind of text
Guys this is driving me crazy.. I am amateur in the mod_rewrite topic.. I
Guys, I am using dynamic programming approach to solve a problem. Here is a
Guys, I'm trying to write xpath or css to find/click on list element All
guys i have a xml file which is like this: <Point TestFlag=0 id=1 name=Conversation

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.