Operations and Methods TRB CSI › Python Programming › Operations and Methods

Operations and Methods

Dictionary operations என்பது dictionary-ல் data-ஐ: create access add update delete search iterate copy combine செய்ய பயன்படுத்தப்படும் basic actions. Dictionary methods என்பது dictionary object-க்கு Python already provide செய்திருக்கும் bu…

Premium — locked Advanced 20 min 226 cards 31 programs 11 MCQs v3
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

DICTIONARY OPERATIONS AND MET……

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

Dictionary operations என்பது dictionary-ல் data-ஐ: create access add update delete search iterate cop……

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

student = { "name": "Arun", "age": 20, "mark": 85 } M……

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

Dictionary: student = { "name": "Arun", "age": 20, "mark": 85 } இதைக் ஒரு student record என்று நினைத்துக்கொள்……

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

student = { "name": "Ravi", "age": 21, "mark": 90 } Structu……

Premium This card is part of the premium lesson. Unlock
Explanation
Operation 1 create dictionary

Dictionary create செய்ய: stud……

Premium This card is part of the premium lesson. Unlock
Program
or
Explanation
Operation 2 access value

Value access செய்ய key பயன்படுத்த வேண்டும……

Premium This card is part of the premium lesson. Unlock

Here: student → dictionary "n……

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

Another method: print(student……

Premium This card is part of the premium lesson. Unlock

Difference மிகவும் important. Using: stu……

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

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

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

age key exists → actual value……

Premium This card is part of the premium lesson. Unlock
Explanation
Operation 3 add item

New key-value pair add செய்ய: studen……

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

student["age"] = 20 Python check……

Premium This card is part of the premium lesson. Unlock
Explanation
Operation 4 update value

Existing key-ன் value மாற்ற: student = { ……

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

Same syntax: dictionary[key] =……

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

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

Premium This card is part of the premium lesson. Unlock
Explanation
Operation 5 delete using del

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

Premium This card is part of the premium lesson. Unlock

del removes specified key-val……

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

del student இதனால் entire var……

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

NameError…

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

Dictionary-ல் key இருக்கிறதா என்று check செ……

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

print("city" not in student) ……

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

Dictionary-ல்: "name" in stud……

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

Because "Arun" is a value. To……

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

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

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

Dictionary-ஐ for loop மூலம் iterate செய்யலாம். st……

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

By default dictionary iterati……

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

for key in student.keys(): pr……

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

for value in student.values()……

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

for key, value in student.ite……

Premium This card is part of the premium lesson. Unlock

This is very important.…

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

student.items() produces pairs like: ("name", "Arun") ("age", 20) ("mark", 90) Then: for key, value……

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

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

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

No error.…

Premium This card is part of the premium lesson. Unlock
Comparison
Get vs square bracket

Using: student["mark"] Missing key: KeyError Using:……

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

Returns all keys. student = { "name……

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

for key in student.keys(): pr……

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

Returns all values. print(stu……

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

for value in student.values()……

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

Returns key-value pairs. prin……

Premium This card is part of the premium lesson. Unlock

Each pair is represented like……

Premium This card is part of the premium lesson. Unlock
Important
Most important items use

for key, value in student.ite……

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

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

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

update() adds or modifies mul……

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

student.update({ "age": 20, "……

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

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

Premium This card is part of the premium lesson. Unlock

Existing key update ஆகிறது.…

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

student.update({ "age": 20, "city": "Chenn……

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

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

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

student.pop("mark") does two ……

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

1. Finds "mark" 2. Removes it……

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

student.pop("city") If city key absent: Key……

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

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

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

popitem() returns: (key, valu……

Premium This card is part of the premium lesson. Unlock
Comparison
Pop vs popitem
Explanation
Example

student.pop("age") removes age.……

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

Removes all items. student = { "na……

Premium This card is part of the premium lesson. Unlock

Dictionary variable still exi……

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

student.clear()…

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

Variable exists. But: del stu……

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

Creates a shallow copy. student = { "name":……

Premium This card is part of the premium lesson. Unlock
Comparison
Assignment vs copy

This is important. a = { "mark": 80 } b = a……

Premium This card is part of the premium lesson. Unlock

Why? a ──┐ ├── Same object b ……

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

dictionary.setdefault(key, default_value) If key 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. It d……

Premium This card is part of the premium lesson. Unlock
Comparison
Setdefault vs update
Output
Result

But: student.setdefault("age"……

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

Because setdefault() keeps ex……

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

Creates a new dictionary using a sequence ……

Premium This card is part of the premium lesson. Unlock

All keys receive same value.…

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

keys = ["a", "b", "c"] data =……

Premium This card is part of the premium lesson. Unlock

Default value is: None…

Premium This card is part of the premium lesson. Unlock
Explanation
Operation 9 merging dictionaries

Modern Python supports dictionary mergi……

Premium This card is part of the premium lesson. Unlock
Explanation
Merging with duplicate key

a = { "x": 10 } b = { "x": 50……

Premium This card is part of the premium lesson. Unlock

Right-side dictionary value w……

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

We can also merge using: a.up……

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

a.update(b)…

Premium This card is part of the premium lesson. Unlock
Comparison
Difference
Comparison
Operation 10 comparison

Dictionaries can be compared using ==. ……

Premium This card is part of the premium lesson. Unlock

Dictionary equality mainly checks same key-va……

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

Dictionary can be created using comprehe……

Premium This card is part of the premium lesson. Unlock
Dry Run
Dry run comprehension
Explanation
Nested dictionary operation

students = { "s1": { "name": "Arun", "mark": 90 }……

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

students["s1"]["mark"] = 95 N……

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

for sid, data in students.items(): pr……

Premium This card is part of the premium lesson. Unlock
Output
Possible output
Table
Dictionary method summary
Table
Operations summary
Important
Important difference 1

d["key"] versus: d.get("key") M……

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

d.pop("key") versus: d.popitem() pop(key……

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

d.clear() versus: del d clear() → items……

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

b = a versus: b = a.copy() b = a → sa……

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

keys() returns: keys values() r……

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

d = { "name": "Arun" } print(……

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

KeyError Better: print(d.get(……

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

d = { "a": 1, "b": 2 } for x in d: print(x) Some stude……

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

d = {"a": 1} d.remove("a") Wrong. Dictionary ……

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

d = {"a": 1} d.append({"b": 2}) Wrong. Dict……

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

Trying positional access: d = { "name": "Arun"……

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

Remember dictionary method categories. Reading Methods get() keys() values() items(……

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

Remember this sequence: G K V I G → get() K → keys() V → values() I → ite……

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

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

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

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

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

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

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

d = { "a": 10, "b": 20 } print(d.items(……

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

d = { "a": 10 } x = d.pop("a"……

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

d = { "a": 1, "b": 2 } x = d.……

Premium This card is part of the premium lesson. Unlock

in modern Python insertion-or……

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

What is returned by: d.clear()…

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

clear() modifies the dictiona……

Premium This card is part of the premium lesson. Unlock
Program
Example
TRB Point
Trb point 8

What does this do? d.setdefault("x", 10) If "x"……

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

Difference between get() and setdefault()? get(): d.get("age", 20) Missing key-க்கு 20 retu……

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

No insertion. But:…

Premium This card is part of the premium lesson. Unlock

Insertion happens.…

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

Do keys(), values(), and items() return lists? No. They retur……

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

Which method gets a value wit……

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

Which returns all keys?…

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

Which returns all values?…

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

Which method returns key-valu……

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

Which method removes a specif……

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

Which method removes the last……

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

Which method removes all dict……

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

What does copy() create?…

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

What is output? d = {"a": 10}…

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

What happens? d = {"a": 10}…

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

What is output? d = {}…

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

What does this return? dict.fromkeys(["a", "b"]) ……

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

Predict output: d = { "name": "R……

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

d = { "a": 1 } d["b"] = 2 pri……

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

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

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

d = { "a": 1, "b": 2 } for ke……

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

d = { "a": 1, "b": 2 } x = d.……

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

Dictionary operations mainly include: Create Access Add Update Delete Search Loop Merge Compare Core syntax: ……

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

Dictionary Operations and Methods = Dictionary data-ஐ access, mod……

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