Knowledge Base‎ > ‎Programming‎ > ‎Python‎ > ‎

07 Lists

07.1 / Intro: What are lists?


Lists are variables that can hold multiple values. A list can contain any type of variable (eg. integer, float, string, etc.) but typically a list will hold items of a single type (eg. all integers or strings).

Variables Lists
- has only one value -has multiple values
- can be of any of the built in - can be composed of any type,
types in Python but usually all of the same type
- may have some useful methods, - has a bunch of useful methods
eg. strings have methods, that you will want to use
but integers don’t

Lists use the square brackets - [ and ] for creation and accessing elements. When accessing elements you may use a single integer to obtain a single value. The index value starts at zero with the fist element.

>>> albums = [] # this is an empty list
>>> albums
[]
>>> type(albums)
<type 'list'>
>>> albums = [ "Some Cities", "The Koln Concert", "Stereolab","Brazil Classics 2"] # a list of 4 album names, each is a string
>>> albums[0]
'Some Cities'
>>> albums[1]
'The Koln Concert'
>>> albums[2]
'Stereolab'
>>> albums[3]
'Brazil Classics 2'

A Basic List


Lists are zero indexed. That is you start counting the first element of the list from 0.



Slicing Lists

You can access a sub-section of a list using two indexes seperated by a colon - “:”

Using two integers allows you to slice the list and create smaller list which just contains the elements from 0 up to (but not including) the index 3 element.

For example:

>>> albums = [ "Some Cities", "The Koln Concert", "Stereolab","Brazil Classics 2"]
>>> albums[0:2]
['Some Cities', 'The Koln Concert']

The result is a list itself. The list will contain all of the elements from the first index up to but not including the element of the second index.

>>> albums[1:3]
['The Koln Concert', 'Stereolab']
>>> albums[1:4]
['The Koln Concert', 'Stereolab', 'Brazil Classics 2']

Usually if you want a part of the list from some element to the end you simply leave off the second index.

>>> albums[1:]
['The Koln Concert', 'Stereolab', 'Brazil Classics 2']

You can do the same type of thing with the first index.

>>> albums[:2]
['Some Cities', 'The Koln Concert']

You can use negative numbers as index - the negatives numbers will move backwards from the end of the list - so index -1 will be the last element, -2 the second last, etc.

>>> albums[-1]
'Brazil Classics 2'
>>> albums[-3]
'The Koln Concert'
>>> albums[-3:-1]
['The Koln Concert', 'Stereolab']
>>> albums[-1:-3]
[]

Remember that [] is simple an empty list.

Finding The Length of a List

In [7]: albums = [ "Some Cities", "The Koln Concert", "Stereolab","Brazil Classics 2"]
In [8]: len(albums)
Out[8]: 4

Note: this interpreter session is using iPython - which is part of your portable Python. I recommend you start using it if only for the tab completion.

Exercises: 07.1


Q1. Create a list with exactly the following, each as a string exactly as shown (imagine these are some marks form a test):

75
84
92
62
82
87

Call this list marks. Not mark. Not markz. Just marks.

Q2. Using the list from Q1 - print out the following (in this order):
  • the length of the list
  • the first item
  • the fifth item
  • the second last item (using a negative index)
  • the second last item (using the len function)


Q3. Using the list from Q1 - print out
  • the slice (sub-list) of the second to fourth items
  • the last 3 items


Q4. Add the following items to your glossary:
  • index
  • zero-indexed
  • element
Comments