Collections
About Collections
- An important part of a programming language is the ability to store and manipulate collections of values. For example, a string is a sequence of characters.
- A collection of values can either be ordered or unordered. An ordered collection is a sequence of values. A sequence has a first element, second element, and so on. A string is a sequence of characters.
- Every collection can be iterated using a
for
loop:
for element in collection:
do something with the element
- The
for
loop will repeat the statements in its action block once for each element in the collection
- The variable defined in the
for
(in the example above we called itelement
) will be assigned to the current element value before each iteration of the block
- Sequences will visit their elements in order
- Every collection can be used with the
len
built-in function.
- The collection types built-in to Python are
- Sequences
str
,list
,range
, andtuple
- Unordered collections
set
, as well asdict
and adict
's keys and values
About Sequences
str
,list
,range
, andtuple
are all sequences.
- Every sequence has access to
for
andlen
, because they are collections
sequence[ i ]
to retrieve a particular element from indexi
sequence[ i : j ]
to retrieve a subsequence from indexi
throughj
, not includingj
sequence[ i : ]
to retrieve a subsequence from indexi
through the end of the sequence
sequence[ : j ]
to retrieve a subsequence from the beginning of sequence throughj
, not includingj
list
is a mutable sequence, meaning you can assign its elements new values
- You change a value like this:
sequence[ i ] = 1112
- You remove a value like this:
del sequence[ i ]
- You can also change or remove entire slices (e.g.
del sequence[ i : j ]
orsequence[ i : j ] = [ 1, 2, 3 ]
) but that is rarely done in practice
str
,range
, andtuple
are immutable, meaning you cannot change their values
Collection literals
- A literal
str
is delimited by matching quotes: either"
or'
or'''
or"""
. Its elements are always characters. Example:'Hi'
- A literal
list
is delimited by square brackets and separated by commas. Example:[ 1, 2, 'three', 4.0 ]
- A literal
tuple
is delimited by parentheses and separated by commas. Example:( 1, 2, 'three', 4.0 )
- A literal
range
is created using therange
type as a function. Examples:range( 5 )
,range( 1, 4 )
,range( 3, 9, 2 )
- A literal
set
is delimited by curly braces and separated by commas. Example:{ 1, 2, 'three', 4.0 }
- A literal
dict
is delimited by curly braces and separated by commas (like a set) but also has colons separating key-value pairs. Example:{ 1:'one', 'two':2 }
Casting and concatenating collections
- You can often cast one sequence type to another by using type-names as functions.
- All values of any type (collection or not) can be case into a
str
- All collections can be cast into a
list
,tuple
, orset
- You cannot cast into a
range
ordict
- Some collections can be concatenated with other values of the same type using the
+
operator
'12' + '34'
creates'1234'
[ 1, 2 ] + [ 3, 4 ]
creates[ 1, 2, 3, 4 ]
( 1, 2 ) + ( 3, 4 )
creates( 1, 2, 3, 4 )
+
does not work onrange
s,set
s, ordict
s
About dicts
- The human brain is great with associations; e.g., we typically associate apple with red and January 1st with New Year's Day.
- A
dict
is Python's software equivallent of a collection of associations.
- In particular, a
dict
is unnordered collection of associations.
- The elements of
dict
are called mappings. A mapping associates one object (called the key) with another object (called the value).
- The literal for an empty
dict
is{}
.
- A literal mapping is written using the
:
to separate the key with its value. For example, the variableflora_to_color
defined below is adict
that associates flora with colors.
flora_to_color = { 'spruce': 'green', 'apple': 'red', 'shamrock': 'green',
'banana': 'yellow', 'potato': 'brown', 'eggplant': 'purple' }
- The
len()
function works on adict
.len(
flora_to_color
)
is 6 because the definitionflora_to_color
included six mappings.
- The brackets
[]
operator in conjunction with assignment can be used to put a mapping into adict
. The statement below adds the mappinglemon
toyellow
.
flora_to_color[ 'lemon' ] = 'yellow'
- The keys of a
dict
are always distinct. If there had already a mapping with'lemon'
as a key, the above assignment would replace it.
- As our
flora_to_color
example shows, the values of adict
are not necessarily unique.
- More detailed information on a
dict
is always available in the module documentation section of the website.