Skip to main content
Chemistry LibreTexts

4.9: Dictionary (container)

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

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    clipboard_ec964f670fd46508763c347bd19162e67.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)

    Overview

    Use KEY:VALUE Pair

    • Use Curly Brackets
    • Mutable
    • Unordered
      • use KEY:VALUE pair separated by commas
    • Values can be
      • lists
      • other dictionaries
      • strings, numbers
    • Are an object class with methods

    Simple Dictionary

    z_alkali_dict={"Lithium":3,"Sodium":11,"Potassium":19, "Rubidium":37, "Caesium":55,"Francium":87}
    #call the dictionary
    z_alkali_dict
    Hello world!

    Dictionary of lists

    First, create the lists.  Note, nothing happens as you have to print something, add  the following to the end of the script

    print("list created")

    name_halogen=['fluorine', 'chlorine', 'bromine', 'iodine', 'astatine','tennessine']
    z_halogen=[9,17,35,53,85,117]
    a_halogen=[18.99840316,35.45,79.90,126.9045,209.98715,294.211]
    symbol_halogen=["F","Cl","Br","I","At","Ts"]
    Hello world!

    Make the dictionary and call it

    hal_dict={'name':name_halogen,"atomic_number":z_halogen,"atomic_weight":a_halogen}
    #call it
    hal_dict
            hello world
          

    Use Key to Call Values

    Here we can use a key to get its value

    print("the names of the halogens are ", hal_dict['name'])
            hello world
          

    After running the above code, modify it to get the atomic weights of the halogens

    Use list index numbers

    print("The heaviest halogen is ", hal_dict['name'][-1])
    print("The heaviest halogen is", hal_dict['name'][-1],", it weighs", hal_dict['atomic_weight'][-1],"amu.")
    
    Hello world!

    Methods

    hal_dict['name'][2].upper()
            hello world
          

    Print the keys

    hal_dict.keys()
    Hello world!

    Print the values

    hal_dict.values()
            hello world
          

    Print the

    print(hal_dict)
    print("\n 5*' * '\n")
    hal_dict.items()
    Hello world!

     

     

     

     

     

     

     

     

     

     

     

    Methods

     


    This page titled 4.9: Dictionary (container) is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?