473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

get last two in a length of unknown length?

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
Jul 18 '05 #1
8 1946

"M. Clift" <no***@here.com> wrote in message
news:cg**********@news7.svr.pol.co.uk...
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

Jul 18 '05 #2
"M. Clift" <no***@here.com> wrote in message
news:cg**********@news7.svr.pol.co.uk...
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
Jul 18 '05 #3

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
Jul 18 '05 #4
Thankyou all for your help. Exactly what I needed : )
Jul 18 '05 #5
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

Jul 18 '05 #6
Cheers Phil : )
Jul 18 '05 #7
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

Jul 18 '05 #8
Again, thanks Wes, I appreciate it.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Navin | last post by:
Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp application running on iis 5.0 windows 2000 i use...
0
by: Brian Rivet | last post by:
------=_NextPart_000_000E_01C34700.FE251100 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sorry, I'm a little new at this, here's the statement: CREATE TABLE...
18
by: John Salerno | last post by:
Ok, I wrote this all by myself, which explains why it doesn't work. It is meant to take a number and generate the next number that follows according to the Morris sequence. It works for a single...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
0
by: dba123 | last post by:
I am getting the following error when a sub domain is receiving a shared cookie: I have this in both web.config of each application. The 1.1 application does not have the decryption= value...
4
by: Anthra Norell | last post by:
Hi, I keep working around a little problem with unpacking in cases in which I don't know how many elements I get. Consider this: def tabulate_lists (*arbitray_number_of_lists): table = zip...
1
by: SoFaraway | last post by:
Hi all, In C, to read a line from a file, we need to allocate some fixed length of memory first. However, if the line is longer than the length of the allocated memory, it can't be read correctly....
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
9
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.