FAQ
Hi,
I am trying to strip a string and then remove on the resulting list to
remove a set of characters. It works fine with the python shell.

But after remove the list becomes None, when i am running it from within a
script.

I am guessing it has something to do with the way python handles assignment.
please find the script below*

a ='oe,eune,eueo, ,u'
b = a.split(',')
print b
c = b.remove('oe')
print a

print c


==============================================
Anand Jeyahar
http://sites.google.com/a/cbcs.ac.in/students/anand
==============================================
The man who is really serious,
with the urge to find out what truth is,
has no style at all. He lives only in what is.
~Bruce Lee

Love is a trade with lousy accounting policies.
~Aang Jie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110203/42e26c19/attachment.html>

Search Discussions

  • Gary Herron at Feb 3, 2011 at 3:29 pm

    On 02/03/2011 07:21 AM, anand jeyahar wrote:
    Hi,
    I am trying to strip a string and then remove on the resulting
    list to remove a set of characters. It works fine with the python shell.

    But after remove the list becomes None, when i am running it from
    within a script.

    I am guessing it has something to do with the way python handles
    assignment.
    please find the script below*

    a ='oe,eune,eueo, ,u'
    b = a.split(',')
    print b
    c = b.remove('oe')
    print a

    print c

    On a list, the "remove" method does not create and return a new list --
    instead it removes the element from the list in place. In your code,
    you "print a" and "print c", but you should have done "print b", where
    you will find the result you expect.

    Gary Herron

    ==============================================
    Anand Jeyahar
    http://sites.google.com/a/cbcs.ac.in/students/anand
    ==============================================
    The man who is really serious,
    with the urge to find out what truth is,
    has no style at all. He lives only in what is.
    ~Bruce Lee

    Love is a trade with lousy accounting policies.
    ~Aang Jie
  • Benjamin Kaplan at Feb 3, 2011 at 3:30 pm

    On Thu, Feb 3, 2011 at 10:21 AM, anand jeyahar wrote:
    Hi,
    ??? I am trying to strip a string and then remove on the resulting list to
    remove a set of characters. It works fine with the python shell.

    But after remove the list becomes None, when i am running it from within a
    script.

    I am guessing it has something to do with the way python handles assignment.
    please find the script below*

    a ='oe,eune,eueo, ,u'
    b = a.split(',')
    print b
    c = b.remove('oe')
    The remove method of a list modifies the list in place and doesn't
    return anything (Therefore, it returns None because every
    function/method in Python has to return something). There's no need to
    assign the result to a variable.
  • Stephen Hansen at Feb 3, 2011 at 5:50 pm

    On 2/3/11 7:21 AM, anand jeyahar wrote:
    Hi,
    I am trying to strip a string and then remove on the resulting list
    to remove a set of characters. It works fine with the python shell.

    But after remove the list becomes None, when i am running it from within
    a script.

    I am guessing it has something to do with the way python handles assignment.
    please find the script below*

    a ='oe,eune,eueo, ,u'
    b = a.split(',')
    print b
    c = b.remove('oe')
    As others have stated, the issue is that b.remove('oe') doesn't return b
    or a copy of b, but directly modifies b instead.

    I'll add that you will find that this behavior is consistent throughout
    the list api: the None is more then just a default thing that's returned
    when nothing else is returned, but in this case its also meant as a
    signal to clearly indicate that the list is modified in-place.

    Every once in awhile someone asks for these methods that modify the list
    itself to either return self, or return a copy of the list -- and I'm
    not going to get into that debate -- but the reason for the "None" is to
    make it so you WILL get errors like the above.

    You only run into this situation with mutable data-types by the way:
    strings ALWAYS return a copy or new string, because they can't actually
    modify the string itself.


    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/

    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: signature.asc
    Type: application/pgp-signature
    Size: 487 bytes
    Desc: OpenPGP digital signature
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110203/b314685e/attachment-0001.pgp>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 3, '11 at 3:21p
activeFeb 3, '11 at 5:50p
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase