Dictionary Methods

What You'll Learn: In this tutorial, you'll discover various built-in methods that you can use on dictionaries. These methods help you manage and manipulate dictionary data efficiently.

Dictionary Methods and Descriptions

  1. clear()
    • Description: Removes all the elements from the dictionary.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      myDict.clear()
      print(myDict)
      
    • Output:

       
      {}
      
  2. copy()
    • Description: Returns a copy of the dictionary.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      newDict = myDict.copy()
      print(newDict)
      
    • Output:

       
      {"brand": "Ford", "model": "Mustang", "year": 1964}
      
  3. fromkeys()
    • Description: Returns a dictionary with the specified keys and value.
    • Example Code:

      python
      keys = ('brand', 'model', 'year')
      value = 'unknown'
      myDict = dict.fromkeys(keys, value)
      print(myDict)
      
    • Output:

       
      {'brand': 'unknown', 'model': 'unknown', 'year': 'unknown'}
      
  4. get()
    • Description: Returns the value of the specified key.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      value = myDict.get("model")
      print(value)
      
    • Output:

       
      Mustang
      
  5. items()
    • Description: Returns a list containing a tuple for each key-value pair.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      items = myDict.items()
      print(items)
      
    • Output:

       
      dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
      
  6. keys()
    • Description: Returns a list containing the dictionary's keys.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      keys = myDict.keys()
      print(keys)
      
    • Output:

       
      dict_keys(['brand', 'model', 'year'])
      
  7. pop()
    • Description: Removes the element with the specified key.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      myDict.pop("model")
      print(myDict)
      
    • Output:

       
      {'brand': 'Ford', 'year': 1964}
      
  8. popitem()
    • Description: Removes the last inserted key-value pair.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      myDict.popitem()
      print(myDict)
      
    • Output:

       
      {'brand': 'Ford', 'model': 'Mustang'}
      
  9. setdefault()
    • Description: Returns the value of the specified key. If the key does not exist, insert the key with the specified value.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      value = myDict.setdefault("color", "red")
      print(value)
      print(myDict)
      
    • Output:

       
      red
      {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
      
  10. update()
    • Description: Updates the dictionary with the specified key-value pairs.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      myDict.update({"year": 2020, "color": "red"})
      print(myDict)
      
    • Output:

       
      {'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
      
  11. values()
    • Description: Returns a list of all the values in the dictionary.
    • Example Code:

      python
      myDict = {"brand": "Ford", "model": "Mustang", "year": 1964}
      values = myDict.values()
      print(values)
      
    • Output:

       
      dict_values(['Ford', 'Mustang', 1964])
      

Try It Yourself: Fun Exercises

  1. Manage Your Favorite Books:
    • Create a dictionary of your favorite books and their authors.
    • Use the update() method to add a new book to your dictionary.
  2. Track Your School Subjects:
    • Write a dictionary of your school subjects and their respective teachers.
    • Use the pop() method to remove a subject you no longer study.

Summary:

In this Python tutorial, we learned about various dictionary methods, including clear(), copy(), get(), items(), pop(), update(), and more. These methods help you manage and manipulate dictionary data efficiently. Keep experimenting and have fun with dictionaries in Python!