#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    266
    Rep Power
    14

    Troubles splicing lists


    I am trying to skip the first element of the list build_pass_list[j] when using a for loop. I have been unable to figure out how to splice of the [1:]. I am assuming for now that because it is a sublist it does not have the same properties as the list itself. Python 2.2.2

    Splicing the back end of the lists works.
    Code:
    build_pass_list[j][:1]
    Splicing the front end does not work.
    Code:
    build_pass_list[j][1:])
    Here is the entire for loop
    Code:
    for j in range(len(build_pass_list)):
    
    for k in range(len(build_pass_list[j][1:])):
    [INDENT]
    print k
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Originally Posted by Theeggman
    I am trying to skip the first element of the list build_pass_list[j] when using a for loop. I have been unable to figure out how to splice of the [1:]. I am assuming for now that because it is a sublist it does not have the same properties as the list itself. Python 2.2.2
    Any particular reason you aren't using Python 2.3?

    Splicing the back end of the lists works.
    Code:
    build_pass_list[j][:1]
    Splicing the front end does not work.
    Code:
    build_pass_list[j][1:])
    Could it be because of the stray ')' you have in there? Because slicing is the same from either end. Without specifics on what errors you are getting or what incorrect behavior you are seeing, I'm not sure what's going wrong for you.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    you could use the range function slightly differently:

    Code:
    for x in range(1, len(your_list)):
        ...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    When you say it doesn't work, what does that mean? Does it throw an exception, and if so then what is it? Or does it not print the results you expect? If so then what do you expect, and what does it actually print?

    What is in build_pass_list? If it contains any non-indexible objects then it will throw an exception.

    In the outer loop why do you use range to get an index into the list, instead of stepping through the list elements directly?

    e.g. instead of

    Code:
    for j in range(len(build_pass_list)):
        for k in range(len(build_pass_list[j][1:])):
            print k
    do


    Code:
    for j in build_pass_list:
        for k in range(len(j[1:])):
            print k
    In the inner loop, what do you expect range(len(j[1:])) to produce? It creates a list of the numbers 0 to N-1, where N is the length of the list j. Are you expecting it to produce the numbers 1 to N?

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    BTW, a small point about terminology.

    What you are talking about is slicing lists - extracting a sub-list using the [:] notation.

    Splicing is when a second list is inserted into a list by assigning it to a slice.

    Examples of splicing:
    Code:
    >>> x = range(10)
    >>> x
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x[1:1] = ['foo']  #insert a list 
    >>> x
    [0, 'foo', 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> x[4:7] = ['bar']  #replace a sublist
    >>> x
    [0, 'foo', 1, 2, 'bar', 6, 7, 8, 9]
    >>>
    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    266
    Rep Power
    14
    Group answer:

    Rebbit: Worked like a champ. Exactly what i needed.

    Strike: I'm using 2.2 because I don't have a choice in the matter. Secondly it was not throwing an error it was just not starting the loop from the second element. From playing around on the python command line the syntax I was using appeared to be correct.

    Devcoach: Noted.

    Thanks for the responses. Problem solved.

IMN logo majestic logo threadwatch logo seochat tools logo