How do we refer to the elements of a list? By index number, starting at zero and stopping one short of the number of elements in the list. So a 20 element list has element numbers 0 to 19.
How can we refer to the last element, then? We could write an expression based on the length of the list - so
mylist[len(mylist)-1] in Python
$mylist[@mylist-1] or
$mylist[$#mylist] in Perl.
That's going to get very messy if you want to do a lot of work on the end elements of a list, so (in both Perl and Python), you can refer to members of a list by a negative position number, meaning "number of positions from the end".
So:
mylist[-1] - last element of a list in Python
$mylist[-2] - next to last (penultimate) element of a list in Perl
(written 2007-08-16)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y111 - Python - More on Collections and Sequences [61] Python is a fabulous language - (2004-09-24)
[386] What is a callback? - (2005-07-22)
[633] Copying a reference, or cloning - (2006-03-05)
[899] Python - extend v append on a list - (2006-10-20)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1873] List Comprehensions in Python - (2008-11-06)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[2894] Sorting people by their names - (2010-07-29)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3150] Python dictionaries - mutable and immutable keys and values - (2011-01-29)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3797] zip in Python - (2012-07-05)
[4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
[4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[240] Conventional restraints removed - (2005-03-09)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[968] Perl - a list or a hash? - (2006-12-06)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
Some other Articles
Good to be homeTroy, up state New YorkPython class rattling aroundRegular expressions made easy - building from componentsLast elements in a Perl or Python listHeading Upstate New YorkRuby, Ruby, Ruby. Rails, Rails, Rails.Plastic or ChinaIn the USA for a few daysFrom Leeds