Python Dictionary Methods - Complete Guide
Dictionaries are one of Python’s most powerful and commonly used data structures. They store data as key-value pairs, making them perfect for mapping relationships and fast lookups.
Sample Dictionary
Throughout this guide, we’ll use this example dictionary where keys are words and values are their character counts:
WORDS = {"HELLO": 5, "WORLD!": 6, "PYTHON": 6}
Essential Dictionary Methods
1. Accessing Keys with .keys()
The .keys()
method returns all dictionary keys as a view object:
# Print all words
for word in WORDS.keys():
print(word)
# Output:
# HELLO
# WORLD!
# PYTHON
# Convert to list if needed
word_list = list(WORDS.keys())
print(word_list) # ['HELLO', 'WORLD!', 'PYTHON']
2. Accessing Values with .values()
The .values()
method returns all dictionary values:
# Print all character counts
for count in WORDS.values():
print(count)
# Output:
# 5
# 6
# 6
# Find the maximum word length
max_length = max(WORDS.values())
print(f"Longest word has {max_length} characters")
3. Accessing Key-Value Pairs with .items()
The .items()
method returns key-value pairs as tuples:
# Print both keys and values
for word, length in WORDS.items():
print(f"'{word}' has {length} characters")
# Output:
# 'HELLO' has 5 characters
# 'WORLD!' has 6 characters
# 'PYTHON' has 6 characters
4. Safe Value Access with .get()
Instead of using brackets []
, use .get()
to avoid KeyError exceptions:
guess = input("Enter a word: ")
# Using brackets (can raise KeyError)
if guess in WORDS:
print(f"'{guess}' has {WORDS[guess]} characters")
# Using .get() (safer approach)
char_count = WORDS.get(guess)
if char_count:
print(f"'{guess}' has {char_count} characters")
else:
print(f"'{guess}' not found in dictionary")
# .get() with default value
char_count = WORDS.get(guess, 0)
print(f"'{guess}' has {char_count} characters")
5. Removing Items with .pop()
The .pop()
method removes a key and returns its value:
guess = input("Enter a word to remove: ")
if guess in WORDS:
removed_count = WORDS.pop(guess)
print(f"Removed '{guess}' (had {removed_count} characters)")
print(f"Remaining words: {list(WORDS.keys())}")
else:
print(f"'{guess}' not found")
# .pop() with default value (avoids KeyError)
removed_count = WORDS.pop(guess, None)
if removed_count:
print(f"Removed '{guess}'")
else:
print(f"'{guess}' was not in the dictionary")
6. Removing Last Item with .popitem()
Removes and returns the last inserted key-value pair:
if WORDS:
last_word, last_count = WORDS.popitem()
print(f"Removed last item: '{last_word}' ({last_count} characters)")
7. Clearing All Items with .clear()
Removes all items from the dictionary:
# Clear the entire dictionary
WORDS.clear()
print(WORDS) # Output: {}
8. Adding Multiple Items with .update()
Add multiple key-value pairs at once:
# Add new words
new_words = {"CODE": 4, "DEBUG": 5, "SYNTAX": 6}
WORDS.update(new_words)
# Or update with keyword arguments
WORDS.update(VARIABLE=8, FUNCTION=8)
print(WORDS)
9. Creating Shallow Copy with .copy()
Create a shallow copy of the dictionary:
words_backup = WORDS.copy()
print(f"Original: {WORDS}")
print(f"Copy: {words_backup}")
Practical Example: Word Guessing Game
Here’s a complete example combining multiple dictionary methods:
def word_game():
words = {"PYTHON": 6, "CODE": 4, "DEBUG": 5, "SYNTAX": 6}
score = 0
print("Guess words and earn points equal to their length!")
print(f"Available words: {len(words)}")
while words:
guess = input("\nEnter a word (or 'quit' to exit): ").upper()
if guess == 'QUIT':
break
points = words.pop(guess, None)
if points:
score += points
print(f"Correct! You earned {points} points.")
print(f"Total score: {score}")
if words:
print(f"Words remaining: {len(words)}")
else:
print("Congratulations! You guessed all words!")
else:
print("Word not found or already guessed.")
print(f"\nFinal score: {score}")
# Run the game
word_game()
Key Takeaways
- Use
.keys()
,.values()
, and.items()
to iterate over different parts of dictionaries - Prefer
.get()
over bracket notation for safe key access - Use
.pop()
to remove and retrieve values safely - Dictionary methods return view objects, not lists (except
.pop()
and.popitem()
) - Most dictionary operations are O(1) average case, making them very efficient
Common Patterns
# Check if dictionary is empty
if not my_dict:
print("Dictionary is empty")
# Get all keys as a list
keys_list = list(my_dict.keys())
# Count occurrences
word_count = {}
for word in text.split():
word_count[word] = word_count.get(word, 0) + 1
# Dictionary comprehension with items()
reversed_dict = {v: k for k, v in original_dict.items()}