The full list of list methods is in Section 2.2.6.4 of the Python Library Reference. Here's an idea of how you might use and combine these methods to carry out common operations.
Create a list:
range, e.g. nums = range(10).mishmash = [14, 'a', 4.19,
'Tim', True]Access an item from a list:
mishmash[0] is the first item in the list, i.e.
14mishmash[1] is the second item in the list, i.e.
'a'mishmash[2] is the third item in the list, i.e.
4.19Access a sublist of a list (called a slice in Python):
mishmash[1:4] is the sublist of items in mishmash from position 1 up
to but not including position 4, i.e. ['a', 4.19, 'Tim']
Note: that the range is [inclusive:exclusive] thus mishmash[1:1] is []
Note: a slice always returns a list, so mishmash[1,2] is
['a'] not just 'a'.
Adding items to a list:
mishmash.append(2) appends 2 to the list,
i.e. it adds it to the end of the list so mishmash becomes
[1, 'a', 4.19, 'Tim', True, 2]mishmash.insert(1, 'hi') inserts the string
'hi' into the list in position 1, i.e. after 14
but before 'a'.mishmash = mishmash + ['bye'] appends 'bye'
to the list. (Note the []s around 'bye').Removing items:
del( mishmash[2] ) deletes the item at
position 2 from the list.mishmash.remove( 'a' ) removes the first (and
only the first) occurrence of 'a' from the list.mishmash.pop() removes the last
item in the list.Getting information about a list and its contents:
in can be used to test and see if an item is in a list,
e.g. if 'a' in mishmash:. Note that in has to loop through the list checking to see if each item is 'a' to do that so it is a relatively slow operation.len( mishmash ) returns the length of the list, i.e. the
number of items in the list.mishmash.index( 'Tim' ) returns the position in which the
first occurrence of 'Tim' can be found.mishmash.count( 'Tim' ) counts the number of
'Tim's in the list.Miscellaneous commands you should try out (because they will probably be useful on assignments!):
mishmash.sort()mishmash.reverse()mishmash.extend()2*mishmash