Re: list IndexError

Discussion in 'Python' started by Ishwor, Dec 22, 2004.

  1. Ishwor

    Ishwor Guest

    On Wed, 22 Dec 2004 14:59:32 -0500, Mike C. Fletcher
    <> wrote:

    [snip]

    >
    > Probably the most pythonic approach to this problem when dealing with
    > small lists is this:
    >
    > result = [ item for item in source if item != 'e' ]
    >
    > or, if you're using an older version of Python without list comprehensions:
    >
    > filter( lambda item: item!='e', source )
    >


    I believe lamda's are unnamed functions aren't they?? Still learning... ;-)

    > or even (expanding the list comprehension):
    >
    > result = []
    > for item in source:
    > if item != 'e':
    > result.append( item )
    >
    > The "python"-ness of the solutions is that we are using filtering to
    > create a new list, which is often a cleaner approach than modifying a
    > list in-place. If you want to modify the original list you can simply
    > do source[:] = result[:] with any of those patterns.
    >


    yeah actually i saw what Fedrik had to say above. I created a sliced
    copy of the l & did my homework within the for loop

    >>> l

    ['a', 'b', 'c', 'd', 'e']
    >>> for x in l[:]:

    if x == 'd':
    l.remove('d');

    >>> l

    ['a', 'b', 'c', 'e']

    This code is so clean and looks very healthy. :) Python will live a
    long way because its a cute language. :)

    > If you really do need/want in-place modification, these patterns are
    > quite serviceable in many instances:
    >
    > # keep in mind, scans list multiple times, can be slow
    > while 'e' in source:
    > source.remove('e')
    >
    > or (and this is evil C-like code):
    >
    > for index in xrange( len(source)-1, -1, -1 ):
    > if source == 'e':
    > del source
    >

    thanx Mike, i have tried this C-ish way as well . :) it seemed quiete
    ok for my small list but dont know about huge lists.

    > Keep in mind that, in the presence of threading, any index-based scheme
    > is likely to blow up in your face (even the filtering solutions can
    > produce unexpected results, but they are generally not going to raise
    > IndexErrors due to off-the-end-of-the-list operations).
    >


    Every 2-4 hours i check the c.l.py ... Its so nice that i learn new
    things everyday in this list.. (some are goood but some are extremely
    subjective!!)
    Lastly i personally feel that Python's way of trying things out before
    implementing is really healthy way of programming. Especially towards
    XP oriented projects. :)

    Thanks Fredrik and Mike.
    Ishwor, Dec 22, 2004
    #1
    1. Advertising

  2. Ishwor

    Egor Bolonev Guest

    On Thu, 23 Dec 2004 06:56:02 +1030, Ishwor <> wrote:

    >>>> l

    > ['a', 'b', 'c', 'd', 'e']
    >>>> for x in l[:]:

    > if x == 'd':
    > l.remove('d');
    >
    >>>> l

    > ['a', 'b', 'c', 'e']
    > This code is so clean and looks very healthy. Python will live a
    > long way because its a cute language.


    imho the code is very unhealthy, i would write
    l = ['a', 'b', 'c', 'd', 'e']
    l.remove('d')
    print l

    btw what are you using ';' for
    Egor Bolonev, Dec 22, 2004
    #2
    1. Advertising

  3. Ishwor

    Ishwor Guest

    On Thu, 23 Dec 2004 07:27:52 +1000, Egor Bolonev <> wrote:
    > On Thu, 23 Dec 2004 06:56:02 +1030, Ishwor <> wrote:
    >
    > >>>> l

    > > ['a', 'b', 'c', 'd', 'e']
    > >>>> for x in l[:]:

    > > if x == 'd':
    > > l.remove('d');
    > >
    > >>>> l

    > > ['a', 'b', 'c', 'e']
    > > This code is so clean and looks very healthy. Python will live a
    > > long way because its a cute language.

    >
    > imho the code is very unhealthy, i would write
    > l = ['a', 'b', 'c', 'd', 'e']
    > l.remove('d')
    > print l
    >
    > btw what are you using ';' for


    I am learning Python. I used 'for' specifically so that i could
    iterate over the list and delete the values. It was a dumb one.. thanx
    for pointing it out but i specifically wanted to understand how
    slicing work. :)
    thanks anyway.

    --
    cheers,
    Ishwor Gurung
    Ishwor, Dec 22, 2004
    #3
  4. Ishwor

    M.E.Farmer Guest

    Hello Ishwor ,
    The simpliest way I can explain slicing is that the slices point to the
    spot *between* the items......
    Take this list for example
    slicer = [0,1,2,3,4,5]
    >>> slicer [1]

    1
    >>> slicer [1:2]

    [1]
    >>> slicer [:-1]

    [0,1,2,3,4]
    >>> slicer[2,4]

    [2,3]

    Hth,
    M.E.Farmer
    M.E.Farmer, Dec 22, 2004
    #4
  5. Ishwor

    Steve Holden Guest

    M.E.Farmer wrote:

    > Hello Ishwor ,
    > The simpliest way I can explain slicing is that the slices point to the
    > spot *between* the items......
    > Take this list for example
    > slicer = [0,1,2,3,4,5]
    >
    >>>>slicer [1]

    >
    > 1
    >
    >>>>slicer [1:2]

    >
    > [1]
    >
    >>>>slicer [:-1]

    >
    > [0,1,2,3,4]
    >
    >>>>slicer[2,4]

    >
    > [2,3]
    >

    You can also use a "stride" rather than take every element and (in
    recent Pythons) go backwards:

    Python 2.4 (#1, Dec 4 2004, 20:10:33)
    [GCC 3.3.3 (cygwin special)] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> slicer = [0,1,2,3,4]
    >>> slicer[::2]

    [0, 2, 4]
    >>> slicer[1::2]

    [1, 3]
    >>> >>> slicer[::-1]

    [4, 3, 2, 1, 0]
    >>> slicer[-1::-2]

    [4, 2, 0]
    >>>


    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
    Steve Holden, Dec 23, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ishwor

    list IndexError

    Ishwor, Dec 22, 2004, in forum: Python
    Replies:
    6
    Views:
    398
    Steven Bethard
    Dec 23, 2004
  2. Mike C. Fletcher

    Re: list IndexError

    Mike C. Fletcher, Dec 23, 2004, in forum: Python
    Replies:
    5
    Views:
    279
    Jeff Shannon
    Dec 23, 2004
  3. Ishwor

    Re: list IndexError

    Ishwor, Dec 23, 2004, in forum: Python
    Replies:
    2
    Views:
    267
    Alex Martelli
    Dec 27, 2004
  4. Replies:
    5
    Views:
    654
    Steven Bethard
    Mar 31, 2005
  5. Replies:
    36
    Views:
    1,570
    MonkeeSage
    Oct 11, 2006
Loading...

Share This Page