Skip to content
Home » Functions & Methods of Dictionary

Functions & Methods of Dictionary

Here is a clear, complete, and exam-oriented explanation of the Functions & Methods of Dictionary in Python, perfect for BCA/MCA/B.Tech students.


Functions & Methods of Dictionary in Python

Python provides powerful built-in functions and methods to work with dictionaries efficiently.
These help in accessing, modifying, updating, and removing key–value pairs.

We divide them into:

  1. Built-in Functions used with dictionaries
  2. Dictionary Methods

1. Built-in Functions for Dictionaries

These functions work on various Python data types, including dictionaries.


1. len()

Returns the number of key–value pairs.

d = {"a": 1, "b": 2}
print(len(d))     # 2

2. type()

Returns the type of object.

print(type(d))   # <class 'dict'>

3. str()

Converts dictionary into a readable string format.

print(str(d))

4. dict()

Creates a new dictionary.

d = dict(name="John", age=20)

2. Dictionary Methods (Very Important for Exams)

These methods are used only for dictionary objects.


1. get(key, default)

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

d = {"a": 1, "b": 2}
print(d.get("a"))        # 1
print(d.get("x", "Not found"))  # Not found

2. keys()

Returns all keys as a view object.

print(d.keys())   # dict_keys(['a', 'b'])

3. values()

Returns all values.

print(d.values())  # dict_values([1, 2])

4. items()

Returns key–value pairs as tuples.

print(d.items())  # dict_items([('a', 1), ('b', 2)])

5. update(other_dict)

Adds or overwrites key–value pairs from another dictionary.

d1 = {"a": 1, "b": 2}
d2 = {"b": 3, "c": 4}

d1.update(d2)
print(d1)   # {'a': 1, 'b': 3, 'c': 4}

6. pop(key, default)

Removes the item with the specified key.

d = {"a": 1, "b": 2}
print(d.pop("a"))   # 1

If key not found → returns default value (if provided).


7. popitem()

Removes and returns the last key–value pair (Python 3.7+).

d = {"a": 1, "b": 2}
print(d.popitem())   # ('b', 2)

8. clear()

Removes all items from the dictionary.

d.clear()
print(d)  # {}

9. copy()

Returns a shallow copy of the dictionary.

d1 = {"a": 1, "b": 2}
d2 = d1.copy()

10. setdefault(key, default)

  • If key exists → returns its value
  • If key does not exist → inserts the key with default value
d = {"name": "Ram"}
print(d.setdefault("age", 20))   # Adds age: 20
print(d)                         # {'name': 'Ram', 'age': 20}

11. fromkeys(iterable, value)

Creates a new dictionary with keys from iterable and same value.

keys = ["a", "b", "c"]
new_dict = dict.fromkeys(keys, 0)
print(new_dict)   # {'a': 0, 'b': 0, 'c': 0}

12. __contains__(key) (using in)

Checks if key exists.

print("a" in d)   # True

13. __delitem__(key) (using del)

Deletes an item.

del d["a"]

Full Example of Major Methods

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

print(student.keys())
print(student.values())
print(student.items())

student.update({"age": 22, "city": "Delhi"})
print(student)

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

student.setdefault("course", "BCA")
print(student)

Summary Table of Dictionary Methods

MethodDescription
get()Returns value of key
keys()Returns list of keys
values()Returns list of values
items()Returns list of key–value pairs
update()Adds/updates dictionary
pop()Removes key and returns value
popitem()Removes last key–value pair
clear()Removes all items
copy()Copies dictionary
setdefault()Adds key if not exists
fromkeys()Creates new dictionary from keys