Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Dictionary Methods & List vs Dictionary

Lesson 33 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Dictionary Methods

Method / FunctionPurpose
len(d)Number of key-value pairs
str(d)String representation of the dictionary
clear()Removes all items
copy()Returns a shallow copy
get(key, default)Returns value for key, or default if missing (no error)
update(other)Merges another dict (or key/value pairs) into this one
keys()Returns a view of all keys
values()Returns a view of all values
items()Returns a view of (key, value) pairs
pop(key)Removes key and returns its value
d = {"a": 1, "b": 2, "c": 3}

print(len(d))          # 3
print(str(d))           # "{'a': 1, 'b': 2, 'c': 3}"

d2 = d.copy()            # independent shallow copy
d2["d"] = 4
print(d, d2)               # d unaffected: {'a':1,'b':2,'c':3}  {'a':1,...,'d':4}

print(d.get("z", 0))        # 0 (key missing, default returned)

update()

d = {"a": 1, "b": 2}
d.update({"b": 20, "c": 3})   # existing key "b" updated, new key "c" added
print(d)                       # {'a': 1, 'b': 20, 'c': 3}

clear()

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

---

Difference between List and Dictionary

FeatureListDictionary
Syntax[1, 2, 3]{"a": 1, "b": 2}
AccessBy index (position)By key
OrderOrdered by insertion (indexable)Ordered by insertion (Python 3.7+), accessed by key
DuplicatesAllowedKeys must be unique
MutabilityMutableMutable
Use caseSequential collection of itemsFast key-based lookup (like a real-world dictionary)
Search speedO(n) — linear scanO(1) average — hash-based lookup
# List: access by position
fruits = ["apple", "banana", "mango"]
print(fruits[1])            # 'banana'

# Dictionary: access by key -- much faster for lookups
prices = {"apple": 50, "banana": 30, "mango": 80}
print(prices["banana"])      # 30

Use a list when order/position matters and items are homogeneous. Use a dictionary when you need to look up values quickly by a meaningful label (key).