Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
List slices in Python - 2 and 3 values forms, with an uplifting example

Python's lists are indexed collection objects. That means that they're rather like arrays in that you look up elements by their position numbers, and the number start at a fixed point (0 in the case of Python); they're not totally like arrays in that they are not stored at unchanging sequential memory locations throughout the time they exist, so that you have additional flexibility in being able to extend them, insert elements into the middle, etc, at the expense of a slight loss of efficiency. The Python language and structure used also prevents you going out of bounds as you can (with disasterous consequences if you don't check) in languages such as C and C++.

Python also allows you to select slices from a list to create another list. This is a great way to get the "top ten" when you have sorted objects, or the last few, or something like that. Let's look at a list and some slices:
  elevator = [2,3,4,5,6,7,8,9,10,11,12,14,15]
  print("The whole list")
  print(elevator)

Where I am this week, there's a lift that runs from 2nd floor up to the 15th floor ... except that there is no 13th floor. So there are 13 floors in total, in list postion numbers 0 to 12. Printing out the whole list, as shown in the code above, gave:
  [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15]

With list slices, I can sepcify a start point and a point before which I end - so:
  print(elevator[4:8])
will give me four positions - position numbers 4, 5, 6 and 7 which are floor numbers 6 to 8:
  [6, 7, 8, 9]

I can also specify start and end points that count down from the end (-1 being the last position, and so on), and I can leave out a position number entirely if I want to mean the very end of the list. Thus:
  print(elevator[:8])
  print(elevator[4:])
  print(elevator[4:-1])

Will give me:
  [2, 3, 4, 5, 6, 7, 8, 9]
  [6, 7, 8, 9, 10, 11, 12, 14, 15]
  [6, 7, 8, 9, 10, 11, 12, 14]


A further (two colons, up to 3 values) format of list slices alloes me to specify a step as well. Let's make one of our lift journeys into an express one, starting at the the floor in position number 3 in the list, stopping at every fourth floor thereafter, and nor reaching (stopping short of) the floor in position number 12 in the list. The code is:
  print(elevator[3:12:4])
and the result of running that code give the following journey:
  [5, 9, 14]

Again, I can leave out start and end points in the three value form; if I leave out the step it defaults to 1, which is the same as the two value format. Here are two final examples:
  print(elevator[3::3])
  print(elevator[::2])

and here are the journeys that they cover:
  [5, 8, 11, 15]
  [2, 4, 6, 8, 10, 12, 15]


The complete example's source code is [here], and this is covered on our Learning to program in Python and Python Programming course.

(written 2011-07-06)

 
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)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [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)
  [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)

Y104 - Python - Lists and Tuples
  [383] Overloading of operators on standard objects in Python - (2005-07-19)
  [657] The ternary operator in Python - (2006-03-25)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
  [1789] Looking for a value in a list - Python - (2008-09-08)
  [2280] Creating and iterating through Python lists - (2009-07-12)
  [2284] Strings as collections in Python - (2009-07-12)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [2719] Traffic lights in Python - (2010-04-13)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4722] Embedding more complex code into a named block - (2016-11-04)


Back to
The Anthony trial - Orange County, Florida. Thoughts on conclusion
Previous and next
or
Horse's mouth home
Forward to
Formatting output in Python through str.format
Some other Articles
World Trade Register - Certainly NOT worth 2985 Euros.
Research and development with the help of your tutor or guide
A set of pictures without point
Formatting output in Python through str.format
List slices in Python - 2 and 3 values forms, with an uplifting example
The Anthony trial - Orange County, Florida. Thoughts on conclusion
Gibraltar - said to have a few residents less than Chippenham
Cruising
Repost - some useful pages on our site
Summer Sunday Train Service Starts - Swindon Chippenham and Melksham to Weymouth
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3348_Lis ... ample.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb