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 3879104
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:41:18+00:00 2026-05-19T22:41:18+00:00

I am getting new_tag from a form text field with self.response.get(new_tag) and selected_tags from

  • 0

I am getting new_tag from a form text field with self.response.get("new_tag") and selected_tags from checkbox fields with

self.response.get_all("selected_tags")

I combine them like this:

tag_string = new_tag
new_tag_list = f1.striplist(tag_string.split(",") + selected_tags)

(f1.striplist is a function that strips white spaces inside the strings in the list.)

But in the case that tag_list is empty (no new tags are entered) but there are some selected_tags, new_tag_list contains an empty string " ".

For example, from logging.info:

new_tag
selected_tags[u'Hello', u'Cool', u'Glam']
new_tag_list[u'', u'Hello', u'Cool', u'Glam']

How do I get rid of the empty string?

If there is an empty string in the list:

>>> s = [u'', u'Hello', u'Cool', u'Glam']
>>> i = s.index("")
>>> del s[i]
>>> s
[u'Hello', u'Cool', u'Glam']

But if there is no empty string:

>>> s = [u'Hello', u'Cool', u'Glam']
>>> if s.index(""):
        i = s.index("")
        del s[i]
    else:
        print "new_tag_list has no empty string"

But this gives:

Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    if new_tag_list.index(""):
        ValueError: list.index(x): x not in list

Why does this happen, and how do I work around it?

  • 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-19T22:41:19+00:00Added an answer on May 19, 2026 at 10:41 pm

    1) Almost-English style:

    Test for presence using the in operator, then apply the remove method.

    if thing in some_list: some_list.remove(thing)
    

    The removemethod will remove only the first occurrence of thing, in order to remove all occurrences you can use while instead of if.

    while thing in some_list: some_list.remove(thing)    
    
    • Simple enough, probably my choice.for small lists (can’t resist one-liners)

    2) Duck-typed, EAFP style:

    This shoot-first-ask-questions-last attitude is common in Python. Instead of testing in advance if the object is suitable, just carry out the operation and catch relevant Exceptions:

    try:
        some_list.remove(thing)
    except ValueError:
        pass # or scream: thing not in some_list!
    except AttributeError:
        call_security("some_list not quacking like a list!")
    

    Off course the second except clause in the example above is not only of questionable humor but totally unnecessary (the point was to illustrate duck-typing for people not familiar with the concept).

    If you expect multiple occurrences of thing:

    while True:
        try:
            some_list.remove(thing)
        except ValueError:
            break
    
    • a little verbose for this specific use case, but very idiomatic in Python.
    • this performs better than #1
    • PEP 463 proposed a shorter syntax for try/except simple usage that would be handy here, but it was not approved.

    However, with contextlib’s suppress() contextmanager (introduced in python 3.4) the above code can be simplified to this:

    with suppress(ValueError, AttributeError):
        some_list.remove(thing)
    

    Again, if you expect multiple occurrences of thing:

    with suppress(ValueError):
        while True:
            some_list.remove(thing)
    

    3) Functional style:

    Around 1993, Python got lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed them and submitted working patches*. You can use filter to remove elements from the list:

    is_not_thing = lambda x: x is not thing
    cleaned_list = filter(is_not_thing, some_list)
    

    There is a shortcut that may be useful for your case: if you want to filter out empty items (in fact items where bool(item) == False, like None, zero, empty strings or other empty collections), you can pass None as the first argument:

    cleaned_list = filter(None, some_list)
    
    • [update]: in Python 2.x, filter(function, iterable) used to be equivalent to [item for item in iterable if function(item)] (or [item for item in iterable if item] if the first argument is None); in Python 3.x, it is now equivalent to (item for item in iterable if function(item)). The subtle difference is that filter used to return a list, now it works like a generator expression – this is OK if you are only iterating over the cleaned list and discarding it, but if you really need a list, you have to enclose the filter() call with the list() constructor.
    • *These Lispy flavored constructs are considered a little alien in Python. Around 2005, Guido was even talking about dropping filter – along with companions map and reduce (they are not gone yet but reduce was moved into the functools module, which is worth a look if you like high order functions).

    4) Mathematical style:

    List comprehensions became the preferred style for list manipulation in Python since introduced in version 2.0 by PEP 202. The rationale behind it is that List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

    cleaned_list = [ x for x in some_list if x is not thing ]
    

    Generator expressions were introduced in version 2.4 by PEP 289. A generator expression is better for situations where you don’t really need (or want) to have a full list created in memory – like when you just want to iterate over the elements one at a time. If you are only iterating over the list, you can think of a generator expression as a lazy evaluated list comprehension:

    for item in (x for x in some_list if x is not thing):
        do_your_thing_with(item)
    
    • See this Python history blog post by GvR.
    • This syntax is inspired by the set-builder notation in math.
    • Python 3 has also set and dict comprehensions.

    Notes

    1. you may want to use the inequality operator != instead of is not (the difference is important)
    2. for critics of methods implying a list copy: contrary to popular belief, generator expressions are not always more efficient than list comprehensions – please profile before complaining
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have problem getting text from edittext inside listview when im in last edittext
Can any one help me..?? My problem is that, after getting response from json
I am new to django and have made a method:GET form with checkboxes/text boxes
I am trying to get the text from a page scrape using xpath, now
I'm getting a lot of these exceptions from users: java.lang.OutOfMemoryError: bitmap size exceeds VM
I am getting started with the paperclip plugin, but I can't seem to get
I'm getting this error The prefix '' cannot be redefined from '' to '
I have trouble getting a form element value into a jstl tag. I am
In my application I have an activity which acts as log-in form. After getting
When submitting a form using jQuery and AJAX, I am getting a 403 error

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.