COMP10001 Lecture 4: Week 4
Use [ , ]
-
Mutable
-
Can use indexing, slice, calculate length
-
Via assignment
○
.append() = add item to end
.remove() = remove first instance of a given value
.pop() = remove element of given index value
.sort() = sort contents
Via methods
○
Modify the internals of lists
-
Lists:
mydict = {"a": 1, "b": 2}
-
dictionary[key] = value to add items to a dictionary
-
mydict[key] to access item
-
x = dictionary.pop(key) to delete items from a dictionary
-
key in mydict to test for key membership
-
mydict.clear() to delete the entire contents
-
del mydict[key] to delete an entry without a return value
-
mydict.keys() to see keys
-
mydict.values() to see values
-
mydict.items() to see (key,val)
-
Dictionaries:
sets are like dictionaries without keys
-
a
-
b = set difference
-
a | b = set union
-
a & b = set intersection
-
mutable with .add() and .remove()
-
.intersection()
-
.intersection_update()
-
.difference()
-
.difference_update()
-
Sets:
keys in dictionaries and elements in sets must be "hashable"
-
int
○
float
○
str
○
tuple
○
bool
○
immutable built
-
in objects are hashable
-
list, dict, set cannot be keys or elements
-
Hashable:
Week 4
Saturday, 24 March 2018 5:53 PM
Foundations of Computing Page 1