Deleting characters from a string

Aahz Maruch aahz at netcom.com
Sat Jun 5 22:32:47 CEST 1999


In article <y0jyahye89i.fsf at vier.idi.ntnu.no>,
Magnus L. Hetland <mlh at idt.ntnu.no> wrote:
>
>result = ""
>for char in host:
>    if char not in ' "':
>        result = result+char
>
>host = result

That's a bad way to do it.  At the very least, that code should be
something like:

# There may be a couple of syntax errors below
result = []
for char in host:
    if char not in ' "':
        result.append(char)
host = string.join ( result, "" )

This has *much* better performance if host is at all long.
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

"Why do you like my boyfriend to tie you up and beat me.?"
    -- ELIZA generates a poly moment




More information about the Python-list mailing list