{'abc': 123, 'def': 456}
{'abc': 123, 'def': 456}
Lecture 04
Python dict
s are a heterogenous, ordered *, mutable containers of key value pairs.
Each entry consists of a key (immutable) and a value (anything) - they are designed for the efficient lookup of values using a key.
A dict
is constructed using {}
with :
or via dict()
using tuples,
keys for a dict
must be an immutable object (e.g. number, string, or tuple) and keys do not need to be of a consistent type.
{1: 'abc', 1.1: (1, 1), 'one': ['a', 'n'], (1, 1): <function <lambda> at 0x152979e10>}
The []
operator exists for dict
s but is used for value look up using a key,
Dictionaries are mutable, so it is possible to insert new key value pairs as well as replace the value associated with an existing key.
Dictionaries can be used with for loops (and list comprehensions) but the loop will only iterate over the keys - to get values use values()
or items()
methods.
In Python a set
is a heterogenous, unordered, mutable container of unique immutable elements.
A set
is constructed using {}
(without a :
) or via set()
,
Sets do not use the []
operator for element checking or removal,
Sets have their own special methods for adding and removing elements,
It is possible to use comprehensions with both set
s and dict
s,
{'t', 'l', 'n', 'x', 'm', 'c', 'w', 'p', 'b', 'e', 'r', 'k', 'd', 'g', 'y', 'q', 'h', 'u', 'a', 'z', 'j', 'o', 'i', 'f', ' '}
Note that tuple
comprehensions do not exist,
These are heterogenous, ordered, mutable collections of elements and behave in much the same way as list
s. They are designed to be efficient for adding and removing elements from the beginning and end of the collection.
These are not part of the base language and are available as part of the built-in collections
library. More on libraries next time, but to get access we will need to import the library or the deque
function from the library.
Values may be added via .appendleft()
and .append()
to the beginning and end respectively,
maxlen
deque
s can be constructed with an optional maxlen
argument which determines their maximum size - if this is exceeded values from the opposite side will be dropped.
This is a tool that is used to describe the complexity, usually in time but also in memory, of an algorithm. The goal is to broadly group algorithms based on how their complexity grows as the size of an input grows.
Consider a mathematical function that exactly captures this relationship (e.g. the number of steps in a given algorithm given an input of size n). The Big-O value for that algorithm will then be the largest term involving n in that function.
Complexity | Big-O |
---|---|
Constant | O(1) |
Logarithmic | O(log n) |
Linear | O(n) |
Quasilinear | O(n log n) |
Quadratic | O(\(n^2\)) |
Cubic | O(\(n^3\)) |
Exponential | O(\(2^n\)) |
Generally algorithms will vary depending on the exact nature of the data and so often we talk about Big-O in terms of expected complexity and worse case complexity, we also often consider amortization for these worst cases.
Operation | list (array) | dict (& set) | deque |
---|---|---|---|
Copy | O(n) | O(n) | O(n) |
Append | O(1) | — | O(1) |
Insert | O(n) | O(1) | O(n) |
Get item | O(1) | O(1) | O(n) |
Set item | O(1) | O(1) | O(n) |
Delete item | O(n) | O(1) | O(n) |
x in s |
O(n) | O(1) | O(n) |
pop() |
O(1) | — | O(1) |
pop(0) |
O(n) | — | O(1) |
For each of the following scenarios, which is the most appropriate data structure and why?
A fixed collection of 100 integers.
A stack (first in last out) of customer records.
A queue (first in first out) of customer records.
A count of word occurances within a document.
Sta 663 - Spring 2023