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
clear()- Description: Removes all the elements from the dictionary.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} myDict.clear() print(myDict)Output:
{}
copy()- Description: Returns a copy of the dictionary.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} newDict = myDict.copy() print(newDict)Output:
{"brand": "Ford", "model": "Mustang", "year": 1964}
fromkeys()- Description: Returns a dictionary with the specified keys and value.
Example Code:
pythonkeys = ('brand', 'model', 'year') value = 'unknown' myDict = dict.fromkeys(keys, value) print(myDict)Output:
{'brand': 'unknown', 'model': 'unknown', 'year': 'unknown'}
get()- Description: Returns the value of the specified key.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} value = myDict.get("model") print(value)Output:
Mustang
items()- Description: Returns a list containing a tuple for each key-value pair.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} items = myDict.items() print(items)Output:
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
keys()- Description: Returns a list containing the dictionary's keys.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} keys = myDict.keys() print(keys)Output:
dict_keys(['brand', 'model', 'year'])
pop()- Description: Removes the element with the specified key.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} myDict.pop("model") print(myDict)Output:
{'brand': 'Ford', 'year': 1964}
popitem()- Description: Removes the last inserted key-value pair.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} myDict.popitem() print(myDict)Output:
{'brand': 'Ford', 'model': 'Mustang'}
setdefault()- Description: Returns the value of the specified key. If the key does not exist, insert the key with the specified value.
Example Code:
pythonmyDict = {"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'}
update()- Description: Updates the dictionary with the specified key-value pairs.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} myDict.update({"year": 2020, "color": "red"}) print(myDict)Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
values()- Description: Returns a list of all the values in the dictionary.
Example Code:
pythonmyDict = {"brand": "Ford", "model": "Mustang", "year": 1964} values = myDict.values() print(values)Output:
dict_values(['Ford', 'Mustang', 1964])
Try It Yourself: Fun Exercises
- 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.
- 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!