get last two in a length of unknown length?

Discussion in 'Python' started by M. Clift, Aug 19, 2004.

  1. M. Clift

    M. Clift Guest

    Hi All,

    I have a list of varying length. Would someone know the way to get the last
    two values for this? I can see how this is done with a list that I know the
    length of, but not one thats generated by user input.

    Thanks for any help
     
    M. Clift, Aug 19, 2004
    #1
    1. Advertising

  2. M. Clift

    M. Clift Guest

    that should of course read list of unknown length : )

    "M. Clift" <> wrote in message
    news:cg1494$lvr$...
    > Hi All,
    >
    > I have a list of varying length. Would someone know the way to get the

    last
    > two values for this? I can see how this is done with a list that I know

    the
    > length of, but not one thats generated by user input.
    >
    > Thanks for any help
    >
    >
     
    M. Clift, Aug 19, 2004
    #2
    1. Advertising

  3. M. Clift

    Paul McGuire Guest

    "M. Clift" <> wrote in message
    news:cg1494$lvr$...
    > Hi All,
    >
    > I have a list of varying length. Would someone know the way to get the

    last
    > two values for this? I can see how this is done with a list that I know

    the
    > length of, but not one thats generated by user input.
    >
    > Thanks for any help
    >

    Use negative index values to count backwards from the end of a list or
    tuple.

    x = [ 0, 1, 2, 3, 4]

    x[-1] gives 4
    x[-2] gives 3
    x[-2:] gives [3,4] <- this is called "slice" notation

    You will find *many* more interesting and fun facts in the Python Tutorial.

    -- Paul
     
    Paul McGuire, Aug 19, 2004
    #3
  4. MC> I have a list of varying length. Would someone know the way to get
    MC> the last two values for this?

    mylist[-2:]

    Skip
     
    Skip Montanaro, Aug 19, 2004
    #4
  5. M. Clift

    M. Clift Guest

    Thankyou all for your help. Exactly what I needed : )
     
    M. Clift, Aug 19, 2004
    #5
  6. M. Clift

    Phil Frost Guest

    A negative index counts from the end, so:

    l = [0,1,2,3,4]
    l[-1] == 4

    this works for slices too:

    l[-2:] == [3,4]

    On Thu, Aug 19, 2004 at 03:45:47AM +0100, M. Clift wrote:
    > Hi All,
    >
    > I have a list of varying length. Would someone know the way to get the last
    > two values for this? I can see how this is done with a list that I know the
    > length of, but not one thats generated by user input.
    >
    > Thanks for any help
     
    Phil Frost, Aug 19, 2004
    #6
  7. M. Clift

    M. Clift Guest

    Cheers Phil : )
     
    M. Clift, Aug 19, 2004
    #7
  8. M. Clift

    wes weston Guest

    M. Clift wrote:
    > Hi All,
    >
    > I have a list of varying length. Would someone know the way to get the last
    > two values for this? I can see how this is done with a list that I know the
    > length of, but not one thats generated by user input.
    >
    > Thanks for any help
    >
    >

    M,
    Note this, maybe, unexpected behavior:

    >>> list=[1]
    >>> list[-2:]

    [1]

    >>> list = []
    >>> list[-2:0]

    []

    wes
     
    wes weston, Aug 19, 2004
    #8
  9. M. Clift

    M. Clift Guest

    Again, thanks Wes, I appreciate it.
     
    M. Clift, Aug 19, 2004
    #9
    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. James
    Replies:
    3
    Views:
    348
    Kevin Goodsell
    Nov 27, 2003
  2. Anthra Norell

    unpaking sequences of unknown length

    Anthra Norell, Aug 27, 2006, in forum: Python
    Replies:
    4
    Views:
    314
    Anthra Norell
    Aug 28, 2006
  3. TomServo

    Converting int of unknown length to a string

    TomServo, May 10, 2006, in forum: C Programming
    Replies:
    11
    Views:
    461
    William Ahern
    May 10, 2006
  4. Magcialking
    Replies:
    12
    Views:
    659
    Gernot Frisch
    Aug 31, 2006
  5. Vincent Arnoux
    Replies:
    1
    Views:
    248
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page