6 Python Fundamentals m05 Collections Slides
6 Python Fundamentals m05 Collections Slides
Collections
Monday, 12 August, 13
Collections
tuple
str
range
list
dict
set
Monday, 12 August, 13
tuple
heterogeneous immutable sequence
Monday, 12 August, 13
tuple
• Delimited by parentheses
• Items separated by commas
• Element access with square brackets
and zero-based index t[index]
• len(t) for number of elements
• Iteration with for loop
• Concatenation with + operator
• Repetition with * operator
Monday, 12 August, 13
tuple
p le s c a n co n ta in a n y ty pe o f
•tu
object
• n e s t e d tu p les
h a in s q u are-brac k et s indexing
• c
to access inner elements
Monday, 12 August, 13
tuple
• Can’t use one object in
parentheses as a single
element tuple
• For a single element tuple
include a trailing comma
• The empty tuple is simply
empty parentheses
Monday, 12 August, 13
tuple m e n t s
l f or o n e o r m o r e e le
t h e s e sa r e o p t io n a
it in g p a r en
• Delim s e f ul f o r m ultip le r e t u rn v a lu e s
a m e d r e f e r e n c e s
T u p le s a r e u ir e c t ly in t o n
• g a ll o w s u s t o d e s tr u c t u re d
T u p le u n p ackin
•
Monday, 12 August, 13
tuple
• The in and not in operators can be used with
tuples – and other collection types – for
membership testing
Monday, 12 August, 13
str
homogeneous immutable sequence of
Unicode codepoints (characters)
Monday, 12 August, 13
str
• len(s) gives number of codepoints (characters)
Monday, 12 August, 13
str
• The + operator can be used for
string concatenation.
• Strings are immutable, so the +=
operator re-binds the reference
to a new object.
• Use sparingly – concatenation
with + or += can cause
performance degradation.
Newfoundland
Monday, 12 August, 13
str
et ho d on th e s ep a ra to r s tr in g
• Call the join() m t
i t ( ) to divid e a s tr in g in to a lis
• Use spl on
a n a rg um en t, s p l i t ( ) d iv id es
• Without
whitespace
p ty s ep a ra to r is a n im p or ta nt
• join()-ing on an em a co lle ct io n of s tr in gs
and fast way of conc a te na ti ng
Monday, 12 August, 13
Moment of Zen
Monday, 12 August, 13
str
• The partition() method divides a s
tring into three
around a separator: prefix, separator,
suffix
• Tuple unpacking is useful to destructu
re the result
• Use underscore as a dummy name fo
r the separator
• Underscore convention understood by
many tools
Monday, 12 August, 13
str gs
t ( ) to in s er t va lu es in to s tr in
• Use forma
la cem en t fie ld s d elim it ed by { and }
• Rep l a rg um en ts
es m a tc he d wit h p os it io na
• Integer field nam s eq ue nc e
es ca n be om itt ed if us ed in
• Field nam or d a rg um ents
d s a re m a tc he d w it h ke yw
• Named fiel
Monday, 12 August, 13
str
th ro ug h ke ys or in d ex es w it h
• Access values p la ce m en t fi eld .
squa re br ack et s in th e re
es s a tt ri bu te s us in g d ot in th e
• Acc
replacement field.
en t fi el d m in i- la ng ua ge p ro vi d es
• The replacem fo rm a tt in g op ti on s .
many value a nd a lig nm en t
Monday, 12 August, 13
range
arithmetic progression of integers
Monday, 12 August, 13
range
• stop value is one-past-the-end
• ranges are “half-open”– start is
included but stop is not
• stop value of a range used as start
value of consecutive range
• optional third step value
Monday, 12 August, 13
Constructor Arguments Result
range(5) stop 0, 1, 2, 3, 4
Monday, 12 August, 13
range
✔ r d ir e c t it e ra t io n ov e r it era ble
• Prefe
objects, such as lists
Monday, 12 August, 13
not using r a n g e – e n u m e ra t e
• Prefer enumerate() for counters
at e ( ) yi el ds (i nd ex , va lu e) tu p le s
• en u m e r
Monday, 12 August, 13
list
heterogeneous mutable sequence
Monday, 12 August, 13
0 1 2 3 4 5
Monday, 12 August, 13
0 1 2 3 4 5
list
• Zero and positive integers for
indexing from the front
Monday, 12 August, 13
-6 -5 -4 -3 -2 -1
list e e nd
in d ex f r o m th
N e g a t iv e integers
• is a t in d e x - 1
T h e la s t eleme n t
• - 1 ]
id seq[ l e n ( s e q )
• A v o
Monday, 12 August, 13
start stop
0 1 2 3 4 5
list
• Slicing extracts part of a list
• slice = seq[start:stop]
• Slice range is half-open – stop not
included
Monday, 12 August, 13
start stop
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
show how to index into sequences
list
• Slicing works with negative in
dexes
Monday, 12 August, 13
start stop
0 1 2 3 4 5
list
• Omitting the stop index slices
to the end
Monday, 12 August, 13
start stop
0 1 2 3 4 5
list
• Omitting the stop index slices
to the end
slice_to_end =
seq[start:]
Monday, 12 August, 13
start stop
0 1 2 3 4 5
list m t h e b e g in n in g
t a rt in d ex s lic es f ro
• Omitting the s o p ]
n n i ng = s e q [ : s t
slice _ f r o m _ b e g i
Monday, 12 August, 13
start stop
0 1 2 3 4 5
s[:3]
show how to index into sequences
start stop
0 1 2 3 4 5
s[3:]
show how to index into sequences
list t a r y s li c e s
e s giv e c om p le m e n
Half-op e n r a n g
•
] + s [ x : ] = = s
s[:x
Monday, 12 August, 13
start stop
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
show how to index into sequences
list
• Omitting the start and stop indexes slices
from the beginning to the end – a full slice
full_slice = seq[:]
list
a 0 1
list list
0 1 0 1
1 2 3 4
Monday, 12 August, 13
Copies are shallow >>> a = [ [1, 2], [3, 4] ]
>>>
list
a 0 1
list list
0 1 0 1
1 2 3 4
Monday, 12 August, 13
Copies are shallow >>> a = [ [1, 2], [3, 4] ]
>>>
list
a 0 1
list list
0 1 0 1
1 2 3 4
Monday, 12 August, 13
Copies are shallow >>> a = [ [1, 2], [3, 4] ]
>>> b = a[:]
list
a 0 1
list list
0 1 0 1
1 2 3 4
Monday, 12 August, 13
Copies are shallow >>> a[0] = [8, 9]
list
a 0 1
list
b 0 1
1 2 8 9 3 4
Monday, 12 August, 13
Copies are shallow >>> a[0] = [8, 9]
>>> a[0]
[8, 9]
>>> b[0]
list [1, 2]
>>> a[1].append(5)
a 0 1
list
b 0 1
1 2 8 9 3 4 5
Monday, 12 August, 13
Copies are shallow >>> a
[[8, 9], [3, 4, 5]]
>>>
list
a 0 1
list
b 0 1
1 2 8 9 3 4 5
Monday, 12 August, 13
Copies are shallow >>> a
[[8, 9], [3, 4, 5]]
>>> b
[[1, 2], [3, 4, 5]]
list
a 0 1
list
b 0 1
1 2 8 9 3 4 5
Monday, 12 August, 13
Copies are shallow >>> a
[[8, 9], [3, 4, 5]]
>>> b
[[1, 2], [3, 4, 5]]
list
a 0 1
list
b 0 1
1 2 8 9 3 4 5
Monday, 12 August, 13
Repetition is Shallow >>> s = [ [-1, +1] ] * 5
s
list
0 1 2 3 4
list
0 1
int int
-1 1
Monday, 12 August, 13
list
Finding elements
• index(item) returns the
integer index of the first
equivalent element raises
ValueError if not found
• count(item) returns the
number of matching elements
• The in and not in
operators test for
membership
Monday, 12 August, 13
list
q [ i n d e x ] to re m ove by in dex
• de l s e
m o v e ( i t e m ) to re m ove by va lue;
• seq. r e
raises ValueError if not present
• re m ov e ( ) eq ui va le nt to
del seq[seq.index(item)]
Monday, 12 August, 13
list
ca te na te lis ts w it h + operator
• C on
la ce ex te ns ion w it h + = op er a tor
• In -p
or extend() method.
cc ep t a ny it er a ble ser ie s on th e
• All a
right-hand side.
Monday, 12 August, 13
list
m e n t t o s o r t ( )
• key argu n
c c e p ts a f un c t io
method a
c in g a s o r t k ey f r om • be aware of unintentional side-
for produ effects with in situ rearrangements
an item
Monday, 12 August, 13
list
• sorted() built-in function sorts any
iterable series and returns a list
Monday, 12 August, 13
dict
unordered mapping from
unique, immutable keys
to mutable values
Monday, 12 August, 13
Recap literals:
dict
• delimited by { and }
• key-value pairs comma separated
• corresponding keys and values
joined by colon
• keys must be unique
Monday, 12 August, 13
dict
urls
dict
keys values
str 0 0 str
"Pluralsight" "http://pluralsight.com"
str 0 0 str
"Sixty North" "http://sixty-north.com"
str 0 0 str
"Google" "http://google.com"
str 0 0 str
"Microsoft" "http://microsoft.com"
u t a b le
Must be immutable Arbitrary order Can be m
Monday, 12 August, 13
dict
dict() constructor accepts:
e s er ie s of ke y- va lu e 2 -t up le s
• iterabl
ke ywor d a rg um en ts – requires keys
•
are valid Python identifiers
m a p p in g, s uc h a s a no th er d ic t
•a
Monday, 12 August, 13
dict
Copying
( ) for co p yi ng d ic tion ari es
• d . c o p y
• or simply dict(d) constructor
Monday, 12 August, 13
dict
Updating a dictionary
en d a d ic ti ona ry w it h u p d a t e ( )
• E xt
• update replaces values
corresponding to duplicate keys
Monday, 12 August, 13
dict
Iteration
• Iteration is over keys
• Get corresponding value with
d[key] lookup
• Remember! Order is arbitrary!
Monday, 12 August, 13
dict
Iteration
• Use values() for an iterable view
onto the series of values.
• No efficient way to get the key
corresponding to a value
• keys() method gives iterable view
onto keys - not often needed.
Monday, 12 August, 13
dict
Iteration
• Use items() for an iterable view
onto the series of key-value tuples.
• Use with tuple unpacking.
Monday, 12 August, 13
dict
Membership
• The in and not in operators work
on the keys.
Monday, 12 August, 13
dict
Removal
• Use del keyword to remove by key
del d[key]
Monday, 12 August, 13
dicilitty
Mutab
• keys must be immutable
• values may be mutable
• The dictionary itself is mutable
Monday, 12 August, 13
Pretty printing
on S ta nd ar d Li br ar y pp ri nt
• Py th
module
B e ca re fu l no t to re bind the module
•
reference!
Kn ow s ho w to pr ett y- pr int all built-in
•
data structures, including dict
Monday, 12 August, 13
Collections
tuple
str
range
list
dict
set
Monday, 12 August, 13
set
unordered collection of
unique, immutable objects
Monday, 12 August, 13
Literals
set
• delimited by { and }
• single comma separated items
• empty {} makes a dict, so for empty
set use the set() constructor.
Monday, 12 August, 13
ser atccepts:
set() constructo
it era bl e s er ie s of va lues
•
d up lic a te s a re d is ca rded
•
of te n us ed s p ec ifi ca lly to remove
•
duplicates – not order preserving
Monday, 12 August, 13
set
Adding elements
• add(item) inserts a single element
• Duplicates are silently ignored
• For multiple elements use
update(items) passing any
iterable series
Monday, 12 August, 13
set
Removing elements
• remove(item) requires that item is
present, otherwise raises KeyError.
• discard(item) always succeeds.
Monday, 12 August, 13
set
Copying
• s.copy() method.
• Use constructor: set(s)
• Copies are shallow!
Monday, 12 August, 13
set algebra
Monday, 12 August, 13
Monday, 12 August, 13
Monday, 12 August, 13
Hydrogen Cyanide (HCN)
Monday, 12 August, 13
Phenylthiocarbamide (PTC)
Monday, 12 August, 13
O B A AB
Monday, 12 August, 13
Union
set • s.union(t) method.
commutative
•
Monday, 12 August, 13
Intersection
• s.intersection(t)
set method.
• commutative
Monday, 12 August, 13
Difference
set •
•
s.difference(t) method.
non-commutative
Monday, 12 August, 13
Symmetric Difference
set • s.symmetric_difference(t)
method.
• commutative
Monday, 12 August, 13
set
Subset
s.issubset(t) method.
t
s
Monday, 12 August, 13
set
Superset
s.issuperset(t) method.
s
t
Monday, 12 August, 13
seintt
Disjo
s.isdisjoint(t) method.
t s
Monday, 12 August, 13
seintt
Disjo
s.isdisjoint(t) method.
t s
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collection Protocols
Monday, 12 August, 13
Collections Summary
Lists are heterogeneous mutable sequence types
Negative indexes work backwards from the end.
Slicing allows us to copy all or part of a list.
The full slice is a common idiom for copying lists, although the copy() method and
list() constructor are less obscure.
List (and other collection) copies are shallow.
List repetition is shallow.
Dictionaries map immutable keys to mutable values
Iteration and membership testing is done with respect to the keys.
Order is arbitrary
The keys(), values() and items() methods provide views onto different aspects of a
dictionary, allowing convenient iteration.
Sets store an unordered collection of unique elements
Sets support powerful and expressive set algebra operations and predicates.
Protocols such as iterable, sequence and container characterise the
collections.
Monday, 12 August, 13