6. Dictionaries#

A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type.

You can think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value. The association of a key and a value is called a key-value pair or sometimes an item.

A empty dictionary can be created using the dict function.

D = dict()

or just by using {}

D = {}

key value pairs can be added to dictionary using [].

D["esdger"] = "dijksta"
D["ada"] = "lovelace"

A value can be found in a dictionary using the key

D["ada"]
'lovelace'

and the value can be changed.

D["ada"] = "Byron"
print(D)
{'esdger': 'dijksta', 'ada': 'Byron'}

6.1. Exercise 1#

Do dictionary keys have to be of the same type ?

6.2. Solution 1#

Hide code cell source

D[3.1] = "pi"

6.3. Exercise 2#

Do dictionary values have to be of the same type ?

6.4. Solution 2#

Hide code cell source

D[2.1] = 7.2

6.5. Exercise 3#

Do dictionary values have to be unique ?

6.6. Solution 2#

Hide code cell source

D[2.2] = 7.1

6.7. Exercise 4#

What types can be used as keys in a dictionary ? Experiment and find out.

Dictionaries can be created with content using {:}

DD = {1 : 3,"a" : (1,True)}
print(DD)
{1: 3, 'a': (1, True)}

The in operator can be used to find if a value is in sequence, be it a list, tuple, or dictionary.

L = [1,2,3]
2 in L
True
T = (1,"hello",True)
False in T
False

In a dictionary, in operates on the keys, not the values.

D = {"a" : 1,(2,3) : True}
(2,3) in D
True

The in operator uses different algorithms for lists and dictionaries. For lists, it uses a search algorithm. As the list gets longer, the search time gets longer in direct proportion to the size of the list. For dictionaries, Python uses an algorithm called a hashtable that has a remarkable property: the in operator takes about the same amount of time no matter how many items there are in a dictionary.

6.8. Exercise 5#

Install the numpy library using pip.

6.9. Solution 5#

Hide code cell source

!python -m pip install numpy

Hide code cell output

Collecting numpy
  Downloading numpy-1.21.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.9 MB)
     |████████████████████████████████| 15.9 MB 7.0 MB/s eta 0:00:01
?25hInstalling collected packages: numpy
Successfully installed numpy-1.21.2
WARNING: You are using pip version 21.2.3; however, version 21.3 is available.
You should consider upgrading via the '/home/grosedj/.pyenv/versions/3.10.0/bin/python -m pip install --upgrade pip' command.

6.10. Exercise 6#

import the numpy library. Use help to find out about the poisson function in the random module of numpy. Note - the numpy library is discussed in more detail in a seperate chapter.

6.11. Solition 6#

Hide code cell source

import numpy
help("numpy.random.poisson")

Hide code cell output

Help on built-in function poisson in numpy.random:

numpy.random.poisson = poisson(...) method of numpy.random.mtrand.RandomState instance
    poisson(lam=1.0, size=None)
    
    Draw samples from a Poisson distribution.
    
    The Poisson distribution is the limit of the binomial distribution
    for large N.
    
    .. note::
        New code should use the ``poisson`` method of a ``default_rng()``
        instance instead; please see the :ref:`random-quick-start`.
    
    Parameters
    ----------
    lam : float or array_like of floats
        Expected number of events occurring in a fixed-time interval,
        must be >= 0. A sequence must be broadcastable over the requested
        size.
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
        a single value is returned if ``lam`` is a scalar. Otherwise,
        ``np.array(lam).size`` samples are drawn.
    
    Returns
    -------
    out : ndarray or scalar
        Drawn samples from the parameterized Poisson distribution.
    
    See Also
    --------
    Generator.poisson: which should be used for new code.
    
    Notes
    -----
    The Poisson distribution
    
    .. math:: f(k; \lambda)=\frac{\lambda^k e^{-\lambda}}{k!}
    
    For events with an expected separation :math:`\lambda` the Poisson
    distribution :math:`f(k; \lambda)` describes the probability of
    :math:`k` events occurring within the observed
    interval :math:`\lambda`.
    
    Because the output is limited to the range of the C int64 type, a
    ValueError is raised when `lam` is within 10 sigma of the maximum
    representable value.
    
    References
    ----------
    .. [1] Weisstein, Eric W. "Poisson Distribution."
           From MathWorld--A Wolfram Web Resource.
           http://mathworld.wolfram.com/PoissonDistribution.html
    .. [2] Wikipedia, "Poisson distribution",
           https://en.wikipedia.org/wiki/Poisson_distribution
    
    Examples
    --------
    Draw samples from the distribution:
    
    >>> import numpy as np
    >>> s = np.random.poisson(5, 10000)
    
    Display histogram of the sample:
    
    >>> import matplotlib.pyplot as plt
    >>> count, bins, ignored = plt.hist(s, 14, density=True)
    >>> plt.show()
    
    Draw each 100 values for lambda 100 and 500:
    
    >>> s = np.random.poisson(lam=(100., 500.), size=(100, 2))

6.12. Exercise 7#

Generate a sample of size 100 from a poisson distribution with \(\lambda = 2\).

6.13. Solution 7#

Hide code cell source

S = numpy.random.poisson(2,100)
print(S)

Hide code cell output

[6 2 1 1 3 0 3 2 4 7 6 4 1 1 4 4 2 1 5 4 2 2 1 2 3 2 3 2 3 3 2 3 1 2 1 2 0
 2 2 3 4 3 0 1 2 2 0 0 1 3 3 1 2 4 0 5 1 3 1 5 0 4 4 2 3 0 5 0 0 2 3 1 1 2
 2 1 3 4 1 1 3 4 0 1 3 3 3 1 3 0 4 0 1 3 1 2 5 1 2 4]

6.14. Exercise 8#

Write a function that uses a dictionary to “bin” a sample of size \(n\) drawn from a poisson distribution with rate \(\lambda\).

6.15. Solution 8#

Hide code cell source

def bin(n,lam) :
    D = {}
    S = numpy.random.poisson(lam,n)
    for s in S :
        if s not in D:
            D[s] = 1
        else:
            D[s] += 1
    return D

bin(10,3)

Hide code cell output

{2: 3, 3: 1, 0: 1, 4: 3, 1: 1, 5: 1}

6.16. Exercise 9#

Spend 5 minutes thinking of as many use cases for dictionary as you can.

For each use case explain why you would use a dictionary and not a list.

How would you describe a dictionary “mathematically” ? How does this description differ (if at all) from a “mathematical” description of a list ?