Skip to main content
Mathematics LibreTexts

17.2.2: Dictionaries

  • Page ID
    82100
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Colors of Fruits

    In Python and SageMath, a dictionary is a convenient data structure for establishing a relationship between sets of data. From the point of view of this text, we can think of a dictionary as a concrete realization of a relation between two sets or on a single set. A dictionary resembles a function in that there is a set of data values called the keys, and for each key, there is a value. The value associated with a key can be almost anything, but it is most commonly a list.

    To illustrate the use of dictionaries, we will define a relationship between colors and fruits. The keys will be a set of colors and values associated with each color will be a list of fruits that can take on that color. We will demonstrate how to initialize the dictionary and how to add to it. The following series of assignments have no output, so we add a print statement to verify that this cell is completely evaluated.

    fruit_color={}
    fruit_color['Red']=['apple','pomegranate','blood orange']
    fruit_color['Yellow']=['banana','apple','lemon']
    fruit_color['Green']=['apple','pear','grape','lime']
    fruit_color['Purple']=['plum','grape']
    fruit_color['Orange']=['orange','pineapple']
    print('done')
    

    We distinguish a color from a fruit by capitalizing colors but not fruit. The keys of this dictionary are the colors. The keys() method returns an interator; so to get a list of keys we wrap the result with list().

    list(fruit_color.keys())
    

    As an afterthought, we might add the information that a raspberry is red as follows. You have to be careful in that if 'Red' isn't already in the dictionary, it doesn't have a value. This is why we need an if statement.

    if 'Red' in fruit_color:
        fruit_color['Red']=fruit_color['Red']+['raspberry']
    else:
        fruit_color['Red']=['raspberry']
    fruit_color['Red']
    

    A dictionary is iterable, with an iterator taking on values that are the keys. Here we iterate over our dictionary to output lists consisting of a color followed by a list of fruits that come in that color.

    for fruit in fruit_color:
        print([fruit,fruit_color[fruit]])
    

    We can view a graph of this relation between colors and fruits, but the default view is a bit unconventional.

    DiGraph(fruit_color).plot()
    

    With a some additional coding we can line up the colors and fruits in their own column. First we set the positions of colors on the left with all \(x\)-coordinates equal to -5 using another dictionary called vertex_pos.

    vertex_pos={}
    k=0
    for c in fruit_color.keys():
        vertex_pos[c]=(-5,k)
        k+=1
    vertex_pos
    

    Next, we place the fruit vertices in another column with \(x\)-coordinates all equal to 5. In order to do this, we first collect all the fruit values into one set we call fruits.

    fruits=Set([ ])
    for v in fruit_color.values():
         fruits=fruits.union(Set(v))
    k=0
    for f in fruits:
        vertex_pos[f]=(5,k)
        k+=1
    vertex_pos
    

    Now the graph looks like a conventional graph for a relation between two different sets. Notice that it's not a function.

    DiGraph(fruit_color).plot(pos=vertex_pos,vertex_size=1)
    

    17.2.2: Dictionaries is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?