Skip to main content
Engineering LibreTexts

9.4: Looping and Dictionaries

  • Page ID
    8638
  • \( \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}}\)

    If you use a dictionary as the sequence in a for statement, it traverses the keys of the dictionary. This loop prints each key and the corresponding value:

    Code 9.4.1 (Python)
    %%python3
    
    counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
    for key in counts:
        print(key, counts[key])
    
    

    Here's what the output looks like:

    jan 100
    chuck 1
    annie 42

    Again, the keys are in no particular order.

    We can use this pattern to implement the various loop idioms that we have described earlier. For example if we wanted to find all the entries in a dictionary with a value above ten, we could write the following code:

    Code 9.4.1 (Python)
    %%python3
    
    counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
    for key in counts:
        if counts[key] > 10 :
            print(key, counts[key])
    
    

    The for loop iterates through the keys of the dictionary, so we must use the index operator to retrieve the corresponding value for each key. Here's what the output looks like:

    jan 100
    annie 42

    We see only the entries with a value above 10.

    If you want to print the keys in alphabetical order, you first make a list of the keys in the dictionary using the keys method available in dictionary objects, and then sort that list and loop through the sorted list, looking up each key and printing out key-value pairs in sorted order as follows:

    Code 9.4.1 (Python)
    %%python3
    
    counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
    lst = list(counts.keys())
    print(lst)
    lst.sort()
    for key in lst:
        print(key, counts[key])
    
    

    Here's what the output looks like:

    ['jan', 'chuck', 'annie']
    annie 42
    chuck 1
    jan 100

    First you see the list of keys in unsorted order that we get from the keys method. Then we see the key-value pairs in order from the for loop.


    This page titled 9.4: Looping and Dictionaries is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Chuck Severance.

    • Was this article helpful?