Dictionary என்பது Python-ல் data-ஐ key : value pair format-… TRB CSI › Python Programming › Dictionaries

Dictionary என்பது Python-ல் data-ஐ key : value pair format-…

Dictionary என்பது Python-ல் data-ஐ key : value pair format-ல் store செய்ய பயன்படும் built-in collection data type. Simple example: student = { "name": "Arun", "age": 20, "mark": 85 } இதில்: "name" → "Arun" "age" → 20 "mark" → 85 Left side:…

Premium — locked Advanced 26 min 230 cards 34 programs 14 MCQs v3
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

DICTIONARIES - Python…

Premium This card is part of the premium lesson. Unlock
Definition

Dictionary என்பது Python-ல் data-ஐ key : value pair format-ல் store செய்ய பயன்படும் built-in collection data ……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

List-ல் values index number ம……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

student = ["Arun", 20, 85] இதில்: student[0] → Arun student[1] → 20 student[2] → 85 ஆனால் index 0, 1, 2 பார்த……

Premium This card is part of the premium lesson. Unlock
Explanation
Real world idea

ஒரு student record: Name = Arun Age = 20 Mark = 85 City = Chennai Python dictionary: ……

Premium This card is part of the premium lesson. Unlock
Flow

Dictionary-ஐ இப்படிப் புரிந்துகொள்ளுங்கள்: Dictionary ↓ Key : ……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

student["mark"]…

Premium This card is part of the premium lesson. Unlock
Output
Result
Explanation

Basic syntax: dictionary_name……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Dictionary uses: { } Curly braces. Each pair uses: : Co……

Premium This card is part of the premium lesson. Unlock
Explanation

student = { student என்ற dictionary உருவாக்குகிறோம். "name": "Kumar"……

Premium This card is part of the premium lesson. Unlock
Explanation
Key and value

Dictionary-ன் அடிப்படை concep……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இதில்: brand → key Toyota → v……

Premium This card is part of the premium lesson. Unlock
Explanation
Accessing dictionary values

Dictionary value access செய்ய key பயன்படுத்த வேண்ட……

Premium This card is part of the premium lesson. Unlock
Program
Another
Important
Important difference from list

List: data = ["Python", 25] Access: data[0] Dictionary: data = { "la……

Premium This card is part of the premium lesson. Unlock
Explanation
Dictionary is mutable

Dictionary is mutable. Mutable means: ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

student["mark"] = 95…

Premium This card is part of the premium lesson. Unlock

Old value: 80 New value: 95…

Premium This card is part of the premium lesson. Unlock
Explanation
Add new item

Existing dictionary-க்கு புதிய key-value pair add செய……

Premium This card is part of the premium lesson. Unlock

Important rule: If key does no……

Premium This card is part of the premium lesson. Unlock
Explanation
Update existing item

If key already exists: studen……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

student["age"] = 21…

Premium This card is part of the premium lesson. Unlock
Comparison
Add vs update

Same syntax: dictionary[key] = value But beha……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

student["city"] = "Madurai" city இல்லை……

Premium This card is part of the premium lesson. Unlock
Explanation
Duplicate keys

Dictionary keys must be uniqu……

Premium This card is part of the premium lesson. Unlock
Program
Example

Why? Same key: "name" twice பயன்படுத்தப்பட்ட……

Premium This card is part of the premium lesson. Unlock
Important

Dictionary: Duplicate keys → ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Both values same. No problem.……

Premium This card is part of the premium lesson. Unlock
Explanation
Dictionary keys data types

Dictionary key commonly: String Integer Tuple போன்ற immutable typ……

Premium This card is part of the premium lesson. Unlock
Explanation
String key

Most commonly string key use செய்வோம். empl……

Premium This card is part of the premium lesson. Unlock
Explanation
Integer key

subjects = { 1: "Tamil", 2: "……

Premium This card is part of the premium lesson. Unlock
Important

subjects[1] works. But: subje……

Premium This card is part of the premium lesson. Unlock
Explanation
Values can have any type

Dictionary values can be almost any Python object. data = { "name": "Arun", "a……

Premium This card is part of the premium lesson. Unlock
Explanation
Empty dictionary

Empty dictionary: data = {} C……

Premium This card is part of the premium lesson. Unlock
Explanation
Dict function

Dictionary can also be created using dic……

Premium This card is part of the premium lesson. Unlock
Explanation
Type check

student = { "name": "Arun" } ……

Premium This card is part of the premium lesson. Unlock
Explanation
Len

len() gives number of key-value pairs. stud……

Premium This card is part of the premium lesson. Unlock

Why? Three pairs: name : Arun……

Premium This card is part of the premium lesson. Unlock
Explanation
Get method

Normally: student["name"] value……

Premium This card is part of the premium lesson. Unlock
Program
Example
Comparison
Square bracket vs get

This is very important. Suppose key not av……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

KeyError But: print(student.g……

Premium This card is part of the premium lesson. Unlock

So: dictionary[key] → missing key cau……

Premium This card is part of the premium lesson. Unlock
Explanation
Get with default value

get() method-க்கு default value கொடுக்க……

Premium This card is part of the premium lesson. Unlock

Because "age" key இல்லை. Anot……

Premium This card is part of the premium lesson. Unlock
Explanation
Keys method

All keys get செய்ய: student = { "name": "Arun", "age": 20……

Premium This card is part of the premium lesson. Unlock
Explanation
Values method

All values: print(student.val……

Premium This card is part of the premium lesson. Unlock
Explanation
Items method

Both keys and values get செய்……

Premium This card is part of the premium lesson. Unlock

Notice: Each pair returned as a……

Premium This card is part of the premium lesson. Unlock
Remember

keys() → keys only values() →……

Premium This card is part of the premium lesson. Unlock
Explanation
For loop over dictionary

Dictionary loop மிகவும் important. student =……

Premium This card is part of the premium lesson. Unlock
Important

By default dictionary loop re……

Premium This card is part of the premium lesson. Unlock
Explanation
Why only keys

When writing: for x in student: Python treats it ……

Premium This card is part of the premium lesson. Unlock
Explanation
Loop through keys

Explicitly: for key in studen……

Premium This card is part of the premium lesson. Unlock
Explanation
Loop through values

for value in student.values()……

Premium This card is part of the premium lesson. Unlock
Explanation
Loop through items

Key and value both: for key, ……

Premium This card is part of the premium lesson. Unlock

This is one of the most impor……

Premium This card is part of the premium lesson. Unlock
Explanation
How items loop works

Suppose: student.items() produces pairs: ("name", "Arun") ("age", 20) ("mark", 90) Loop: for key, v……

Premium This card is part of the premium lesson. Unlock
Explanation

Execution: First pair: ("A", ……

Premium This card is part of the premium lesson. Unlock
Explanation

Print: A 10 Next: ("B", 20) U……

Premium This card is part of the premium lesson. Unlock
Explanation

Print: B 20 Final output: A 1……

Premium This card is part of the premium lesson. Unlock
Explanation
Check key exists

Use in. student = { "name": "……

Premium This card is part of the premium lesson. Unlock
Program
Another
Important

in checks keys, not values.…

Premium This card is part of the premium lesson. Unlock
Explanation
Check value

To check value: print("Arun" ……

Premium This card is part of the premium lesson. Unlock
Explanation
Not in

print("city" not in student)…

Premium This card is part of the premium lesson. Unlock

if city key is absent.…

Premium This card is part of the premium lesson. Unlock
Explanation
Remove item del

del removes an item using key. student = { "n……

Premium This card is part of the premium lesson. Unlock
Explanation
Pop method

pop() removes specified key and returns its value. stu……

Premium This card is part of the premium lesson. Unlock
Explanation
Popitem

popitem() removes the last inserted key-value pair. studen……

Premium This card is part of the premium lesson. Unlock

Returned value itself is a tu……

Premium This card is part of the premium lesson. Unlock
Explanation
Clear

Remove all items: student.cle……

Premium This card is part of the premium lesson. Unlock

Dictionary still exists, but ……

Premium This card is part of the premium lesson. Unlock
Explanation
Del dictionary

del student Entire variable is d……

Premium This card is part of the premium lesson. Unlock
Comparison
Clear vs del

clear() → removes all items → dictionary ……

Premium This card is part of the premium lesson. Unlock
Explanation
Update method

Multiple values add/update செய்ய: student = { ……

Premium This card is part of the premium lesson. Unlock
Explanation
Update existing key

student = { "name": "Arun", "mark……

Premium This card is part of the premium lesson. Unlock
Explanation
Copy

Dictionary copy: student = { "name": "Arun……

Premium This card is part of the premium lesson. Unlock
Important

a = student does not create an inde……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

b["mark"] = 100…

Premium This card is part of the premium lesson. Unlock

Because: a ──┐ ├── same dicti……

Premium This card is part of the premium lesson. Unlock
Explanation
Nested dictionary

Dictionary inside dictionary: students = { "student1": { "name": ……

Premium This card is part of the premium lesson. Unlock
Explanation
Access nested dictionary

print(students["student1"]["n……

Premium This card is part of the premium lesson. Unlock

Think step-by-step: students["student1"] r……

Premium This card is part of the premium lesson. Unlock
Explanation
Dictionary with list

Dictionary value can be a list. student = { "name……

Premium This card is part of the premium lesson. Unlock
Program
First mark
Explanation
Dictionary with tuple

student = { "name": "Arun", "subjects": ……

Premium This card is part of the premium lesson. Unlock
Explanation
Fromkeys

dict.fromkeys() creates dictionary from keys with same ……

Premium This card is part of the premium lesson. Unlock
Explanation
Setdefault

setdefault() checks a key. If key exists: ex……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation
Setdefault existing key

student = { "name": "Arun", "age……

Premium This card is part of the premium lesson. Unlock

Why? age already exists. So e……

Premium This card is part of the premium lesson. Unlock
Explanation
Dictionary order

Modern Python dictionaries preserve insertion order. data = { "a": 1, "b": 2, "c": 3 } Iteration normally fol……

Premium This card is part of the premium lesson. Unlock
Explanation
Can we access by position

Suppose: student = { "name": "Arun", "age": 20 } This……

Premium This card is part of the premium lesson. Unlock
Program
Example

Here 0 is a key, not a positi……

Premium This card is part of the premium lesson. Unlock
Explanation
Hashable keys

Advanced important concept: Dictionary keys need to be hashable. Usually immutable objects can be keys. Valid……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError: unhashable type: '……

Premium This card is part of the premium lesson. Unlock
Explanation
Tuple as key

Tuple can be a dictionary key……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation
Dictionary comprehension

Similar to list comprehension……

Premium This card is part of the premium lesson. Unlock
Program
Example
Definition
Meaning

Key → x Value → x*x…

Premium This card is part of the premium lesson. Unlock
Dry Run
Dry run dictionary comprehension
Comparison
List vs tuple vs set vs dictionary
Important
Important methods

Main dictionary methods: keys() values() items() get() update() pop……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 1

Wrong key: student = { "name"……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

KeyError: 'age' Better: print……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 2

Wrong assumption: data = {"a": 10, "b": 20} print(data[0]) ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 3

Duplicate key: data = { "x": 10, "……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 4

List as key: data = { [1, 2]:……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError: unhashable type: '……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 5

Do not confuse: {} with empty set. Actual……

Premium This card is part of the premium lesson. Unlock

Empty set requires: set() Thi……

Premium This card is part of the premium lesson. Unlock
Memory Trick

Dictionary remember செய்ய: Dictionary = Word me……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

"name" → "Arun" "age" → 20 So……

Premium This card is part of the premium lesson. Unlock
TRB Point

What is the output? d = { "a"……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 2

d = { "x": 1, "x": 2 } print(……

Premium This card is part of the premium lesson. Unlock

Reason: Dictionary keys must ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 3

d = { "a": 10, "b": 20 } for ……

Premium This card is part of the premium lesson. Unlock
Important

Default dictionary iteration ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 4

What does items() return? d.items() Concep……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 5

What is: type({}) Answer: Not……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 6

What is: type(set()) Answer:…

Premium This card is part of the premium lesson. Unlock
Interview Point

Why dictionary access is fast? Python dictionaries use a hash table internally. When a key is provided: stude……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 2

Can dictionary keys be mutable? Generally, mutable types like list cannot be keys be……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 3

Difference between get() and []? d["key"] Missi……

Premium This card is part of the premium lesson. Unlock
MCQ

Which symbol is used for dict……

Premium This card is part of the premium lesson. Unlock
MCQ

Dictionary stores data as:…

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? d = {"nam……

Premium This card is part of the premium lesson. Unlock
MCQ

What happens? d = {"a": 1} pr……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? d = {"a":……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? d = {"a":……

Premium This card is part of the premium lesson. Unlock
MCQ

Which method returns keys?…

Premium This card is part of the premium lesson. Unlock
MCQ

Which method returns values?…

Premium This card is part of the premium lesson. Unlock
MCQ

What does items() provide?…

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? d = { "a"……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the type? x = {} prin……

Premium This card is part of the premium lesson. Unlock
MCQ

Which creates an empty set?…

Premium This card is part of the premium lesson. Unlock
MCQ

Dictionary keys are generally:…

Premium This card is part of the premium lesson. Unlock
MCQ

Dictionary values:…

Premium This card is part of the premium lesson. Unlock
Mcq 15

What is the output? d = {"x": 1……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 1

Predict output: student = { "name":……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 2

Predict: data = { 1: "A", 2: ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 3

Predict: d = { "a": 10, "b": ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 4

Predict: d = { "a": 10, "b": ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 5

Predict: d = { "a": 1, "b": 2……

Premium This card is part of the premium lesson. Unlock
Summary

Dictionary என்பது Python-ல் data-ஐ: key : value pairs-ஆக store செய்யும்……

Premium This card is part of the premium lesson. Unlock

Main methods: keys() values() items() get() update() pop() popitem() clear() copy() Most important rules: Dic……

Premium This card is part of the premium lesson. Unlock
Explanation
Short description

Dictionary = Key-based mutable collection of key-value pairs. Easy memory: List → Index : Value Dictionary → ……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program