Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Dictionary Methods (Complete Guide for Beginners)

 Python dictionaries come with many built-in methods that make it easy to work with key-value data. These methods help you add, remove, update, access, and manipulate dictionaries efficiently.

In this tutorial, you will learn all important Python dictionary methods with clear examples.


What is a Dictionary Method?

A dictionary method is a built-in function in Python used to perform operations on dictionaries.

Example Dictionary

student = {
    "name": "John",
    "age": 20,
    "grade": "A"
}

1. keys() Method

Returns all keys in the dictionary.

Example

print(student.keys())

Output

dict_keys(['name', 'age', 'grade'])

2. values() Method

Returns all values in the dictionary.

Example

print(student.values())

Output

dict_values(['John', 20, 'A'])

3. items() Method

Returns key-value pairs as tuples.

Example

print(student.items())

Output

dict_items([('name', 'John'), ('age', 20), ('grade', 'A')])

4. get() Method

Returns the value of a key. If key does not exist, returns None or default value.

Example

print(student.get("name"))

Output

John

With Default Value

print(student.get("city", "Not Found"))

Output

Not Found

5. update() Method

Adds or updates dictionary items.

Example

student.update({"city": "London"})
print(student)

Output

{'name': 'John', 'age': 20, 'grade': 'A', 'city': 'London'}

6. pop() Method

Removes a specific key and returns its value.

Example

removed = student.pop("grade")
print(removed)
print(student)

Output

A
{'name': 'John', 'age': 20}

7. popitem() Method

Removes the last inserted item.

Example

item = student.popitem()
print(item)

Output

('grade', 'A')

8. clear() Method

Removes all items from the dictionary.

Example

student.clear()
print(student)

Output

{}

9. copy() Method

Returns a copy of the dictionary.

Example

new_student = student.copy()
print(new_student)

Output

{'name': 'John', 'age': 20, 'grade': 'A'}

10. setdefault() Method

Returns value of a key. If key doesn't exist, inserts it with a default value.

Example

student = {"name": "John"}

student.setdefault("age", 20)

print(student)

Output

{'name': 'John', 'age': 20}

11. fromkeys() Method

Creates a dictionary from keys with a default value.

Example

keys = ["name", "age", "grade"]

new_dict = dict.fromkeys(keys, "Unknown")

print(new_dict)

Output

{'name': 'Unknown', 'age': 'Unknown', 'grade': 'Unknown'}

Looping with Dictionary Methods

Example

for key, value in student.items():
    print(key, ":", value)

Real-World Example: User Profile

user = {
    "username": "admin",
    "email": "admin@example.com"
}

user.update({"role": "manager"})

print(user.get("email"))

Real-World Example: Inventory System

inventory = {
    "Laptop": 10,
    "Mouse": 25
}

inventory.pop("Mouse")
inventory.setdefault("Keyboard", 15)

print(inventory)

Dictionary Methods Summary Table

MethodDescription
keys()Returns all keys
values()Returns all values
items()Returns key-value pairs
get()Gets value safely
update()Adds/updates items
pop()Removes specific item
popitem()Removes last item
clear()Removes all items
copy()Copies dictionary
setdefault()Adds key if missing
fromkeys()Creates new dictionary

Common Mistakes

Mistake 1: Using get() Instead of Direct Access

✔ Safe way:

student.get("name")

❌ Risky way:

student["name"]

Mistake 2: Forgetting pop() Removes Data

student.pop("age")

This permanently removes the key.


Practice Exercise 1

Get the value of "age" using get().


Practice Exercise 2

Add "city": "Paris" using update().


Conclusion

Python dictionary methods are powerful tools for handling data efficiently.

You learned:

  • How to access keys, values, and items
  • How to safely retrieve data using get()
  • How to modify dictionaries using update(), pop(), and setdefault()
  • How to create and copy dictionaries

Mastering dictionary methods is essential for real-world Python programming like APIs, data analysis, and backend systems.




Post a Comment

0 Comments