Lists
Preface
- I expect this letter to be augmented as we learn more about lists
- A description of important list module components is available. Your are strongly advised to check it out.
Basics
- A Python list is an ordered list; that is, there is a first element, second element, and so on.
- Like as it is with strings, the first element of a list had index 0, and the last element of a list has index
n-1
wheren
is the number of elements in the list.
- A new empty list can be created using empty brackets. The statement
stuff = []
makes
stuff
a list with no elements.
- A new list can be created listing values within brackets. The statement
stuff = [ 'abc', 1112, math.pi, ]
initializes
stuff
to be a three-element list, the first element has the string'abc'
as its value, the second element has value 1112, and the last element has valuemath.pi
.
- The
in
andnot in
operations can be used to determine whether a value is or is not an element of a list. Supposestuff
is a list, then
b1 = (x in stuff )
b2 = (x not in stuff )
sets
b1
to beTrue
orFalse
depending whetherx
is an element ofstuff
, and setsb2
to beTrue
orFalse
depending whetherx
is not an element ofstuff
.
- The
[]
(brackets) operator works on lists in the same way as it does on strings.
- If there is just a single integer with the brackets, then the integer is used to access a single element from the list. Suppose
stuff
is a list, thenstuff[ i ]
is the element into the list whose index isi
.
- If there is a
:
within the brackets then a new slice of the list is gotten. Supposestuff
is a list, thenstuff[ i : j ]
is list whose elements are equal to those ofstuff[ i ]
,stuff[ i + 1]
, ...stuff[ j-1 ]
. Like it is with strings, thei
andj
are optional. Ifi
is omitted, then 0 is used in its stead; ifj
is omitted, then the length of the list is used in its stead.
- The built-in function
len()
when given a list as its argument, returns the number of elements in the list. Supposestuff
is a list, then
n = len( stuff )
sets the
n
to be the number of elements instuff
.
- Built-in functions
max()
andmin()
can be used to determine the largest and smallest value in a list. Supposestuff
is a list, then
largest = max( stuff )
smallest = min( stuff )
sets
largest
andsmallest
respectively to the maximum and minimum value instuff
.
- Unlike strings whose character sequences are immutable once created, lists are mutable — the values of individual elements can change, and elements can be deleted, inserted, or added.
- Like strings, lists have many member functions that can inspect and manipulate the list.
- List member function
append()
appends an element to the end of its list. Supposestuff
is a list, then
stuff.append( v )
adds
v
to the end of liststuff
.
- An element value can be changed using assignment. Suppose
stuff
is a list, then
stuff[ x ] = v
sets the
x
th element ofstuff
to have valuev
- An expanded but still partial description of the list module is available. It indicates that there are member functions for adding, removing, inserting, counting, and searching a list.
Patterns
- Computer scientists like the use the word pattern to mean a template for an algorithm. Three patterns are listed below.
- Suppose you want to sum the elements of a list. An accumulator is necessary. The accumulator keeps a running total of the elements considered so far. A summing accumulator is typically initialized to 0 (the running total before any elements have been considered). Suppose
numbers
is a numeric list, then code segment
total = 0
for nbr in numbers :
total = total + nbr
print( total )
prints the sum of the elements in
numbers
.
- Suppose
inputs
is a list of integer numeric strings. A new list whose elements are the numeric counterparts of those strings can be gotten by considering each element ofinputs
and adding its conversion to the new list in turn. The new list before any ofinputs
has been considered should be initialized to an empty list (i.e.,[]
). The conversions can then be accumulated to produce the new list.
numbers = []
for s in inputs :
nbr = int( s )
numbers.append( nbr)
print( numbers )
prints the
inputs
as a list of integers.
- Suppose
numbers
is a list of integers and what we want to do is to update the list so that all values are positive. The following code does not work.
for nbr in numbers :
nbr = abs( nbr )
Why you say? What the code does is repeatedly set
nbr
to be the absolute value of its current value. Doing so does not changenumbers
. However, the following code does work.
n = len( numbers )
for i in range( 0, n ) :
numbers[ i ] = abs( numbers[ i ] )
Why you say again? Because each iteration another list element of
numbers
is updated to be the absolute value of its currrent value.
- This next pattern really belongs with patterns for strings, but what the heck. A new string can be gotten through accumulation. The new string is initialized to the empty string (i.e.,
''
). Characters are added to the new string through an accumulation process.
new_string = ''
for s in stuff
c = ... # depends on what we want out of s
new_string = new_string + c
print( new_string )
prints the new string.
© 2019 Jim Cohoon | Resources from previous semesters are available. |