Professional Documents
Culture Documents
Fundamentals of
Programming
Lesson 3
Lists
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
The material in this communication may be subject to copyright under the Act. Any
further reproduction or communication of this material by you may be the subject of
copyright protection under the Act.
Answer:
It may be useful to have an empty list because
we intend to use it later in the code based on
specific conditions. One scenario (and there
are many) could be asking users to provide
number guesses, and doing some form of
analysis.
List Methods
• In Python, for any built-in data type
(Strings, Boolean, Lists, and so on), there
are built-in functionality that can be used
to achieve specific tasks
• For lists, methods need to adhere to the
following form:
# list_name is our list
# .method() is a method of our list
# This example is for structure illustration,
# it will not work
list_name.method()
List Methods (2)
• In the previous slide, if you attempted to
use list_name.method(), Python would
have returned (assuming list_name was
initialised empty at least)
AttributeError: 'list' object has no attribute 'method'
Element Index
‘Jing’ 0
‘Jim’ 1
‘Kerrie’ 2
‘Rakesh’ 3
• The element with index 1 is ‘Jim’
– We can access this element from a list using square brackets:
students = ['Jing', 'Jim', 'Kerrie', 'Rakesh']
print(students[1]) # prints Jim
• Note, an int type must be used to access the list element
Accessing list elements (3)
students = ['Jing', 'Jim', 'Kerrie', 'Rakesh']
2. grades.pop(specific_index)
grades = [35, 50, 65, 85]
grades.pop(0)
print(grades) # prints [50, 65, 85]
3. del grades[specific_index]
grades = [35, 50, 65, 85]
del grades[0]
print(grades) # prints [50, 65, 85]
Remove list elements (3)
• Additional useful information:
1. grades.remove(value)
a) Uses a value to access an element (instead of index)
b) Removes the first instance of matching element (if there were
multiple 35s, it only removes the first one that matched)
2. grades.pop(specific_index)
a) Uses an index to access an element
b) If specific_index is not provided, it will remove the last
element in the list, when grades.pop() is called
3. del grades[specific_index]
a) Uses an index to access an element
b) del is a keyword to delete objects, as opposed to a specific
List method in (1) and (2) above
• Once an element is removed, the index order of
elements in a list has most likely changed as well
Two Dimensional (2D) Lists
• Recall, that a list can store any type of
data, including a list within a list
– Known as a nested list – this logic can be
applied to create a three dimensional (3D) list
• Recall, from the previous slides in this
workshop that grades and students lists
were used independent of each other
– This did not make sense, as we didn’t know
which student had what grade, we could only
assume that the list order was correct
Two Dimensional (2D) Lists (2)
• Let’s create a new list gradebook that will store
our students and grades lists together:
gradebook = [['Jing', 35], ['Jim', 50], ['Kerrie', 65], ['Rakesh', 85]]
Note:
• list() constructor must be used to convert zip type to list
type
• Each pair within the gradebook_zip list has a tuple type, as
opposed to a list type (in the original gradebook list)
Tuples
• Tuples do the same operation as Lists.
– However, instead declaring using square brackets
[] like Lists, it uses round brackets ()
tuple_students = ('Jing', 'Jim', 'Kerrie', 'Rakesh')
• Built-in function:
# Syntax for a built-in function
builtinfuncion(parameter)
Answer:
Output is: [3, 4, 5, 6, 7, 8]
Because the start index (specifically, negative
index) is -6. It starts at negative index -6
(specifically at number 3), and continues until
the end (specifically at number 8).
Sets
• Just like Lists and Tuples, Sets are used to store
collection of items
– Sets are declared using curly brackets {}
• Important Set considerations:
1. Set items are unchangeable
a) The exception: add and remove items allowed
2. Set items are unordered – items can’t be indexed as there’s
no structure
3. And, the most important use of Sets, it can’t have two items
with the same values. Duplicates are not allowed
a) Lists and Tuples allow for duplicates
*Set items are unchangeable, but you can remove and/or add
items whenever you like.
**As of Python version 3.7, dictionaries are ordered. In Python
3.6 and earlier, dictionaries are unordered.
Source:
https://www.w3schools.com/python/python_dictionaries.asp