bembry.org
Home / Technology / Python / Notes

Lesson 14 Notes: Dictionaries (When a list just won't do)

  • Remember Lists?
    • Lists are collections of items (strings, integers, or even other lists).
    • Each item in the list has an assigned index value.
    • list = ["first", "second", "third"]
    • list[0] == "first"
    • list[1] == "second"
    • list[2] == "third"
  • List Limitations
    • To get a value out of a list, you must know its index position.
    • If you remove an item from a list, the index may change for the other items in the list
    .
  • Dictionary Concept
    • Dictionaries are collections of items that have a "key" and a "value".
    • They are just like lists, except instead of having an assigned index number, you make up the index.
    • list = ["first", "second", "third"]
    • dictionary = {0:"first", 1:"second", 2:"third"}
  • Creating a Dictionary
    • Dictionaries are enclosed by {}
    • To create a dictionary, provide the key and the value.
    • A colon is placed between key and value (key:value)
    • Each key must be unique.
    • Each key:value pair is separated by a comma.
    • espanol = {'uno':'one', 'dos':'two'}
  • Sample Dictionary
      nihon_go = {}
      nihon_go["ichi"] = "one"
      nihon_go["ni"] = "two"
      nihon_go["san"] = "three"
      print nihon_go
      print nihon_go
      {'ichi':'one', 'ni':'two', 'san':'three'}
      {'ichi':'one', 'ni':'two', 'san':'three'}
  • Using a Dictionary
    • To get a value out of a dictionary, you must enter the key.
    • Dictionaries only work one way. You must provide the key to get a value. You cannot provide the value and get the key.
    • Example:
      nihon = {'ichi':'one', 'ni':'two'}
      print nihon['ichi']
      'one'

    • NOTE: This dictionary will translate Japanese to English, but not English to Japanese
  • Editing a Dictionary
    • To change a value pair, reassign it:
      nihon["ichi"] = 1
    • To add a key value pair, enter it like so:
      nihon["shi"] = "Four"
    • To remove a value pair, use del
      del nihon["ichi"]
    • To see if a key exists, use has_key() method
      nihon.has_key("ichi")
    • To copy whole dictionary, use the copy() method
      japanese = nihon.copy()
  • Dictionaries are Mutable
    • Dictionaries are mutable. You do not have to reassign the dictionary to make changes to it.
  • Uses of Dictionaries
    • Dictionaries are useful whenever you have to items that you wish to link together.
    • Making substitutions (replace all x with y)
    • Storing results for quick lookup
    • Program menus
    • Creating mini databases of information
  • Sample Code: A Menu
      import string
      def add(num1,num2):
          print num1+num2
      def mult(num1,num2):
          print num1*num2
      num1 = input("Enter first number: ")
      num2 = input("Enter second number: ")
      menu = {'A':add, 'M':mult}
      print "[A] to add, [M] to multiply: "
      choice = string.upper(raw_input())
      menu[choice](num1,num2)
  • Review
    • How is a dictionary different from a list?
    • How do you create a dictionary?
    • How do you access an entry in a dictionary?
    • How do you add an entry? Delete an entry?
    • What situations might be good for using a dictionary instead of a list?
    Restricted access