Foundations — Nested data structures — list of dicts, dict of lists
1.2.26 · D1· Coding › Introduction to Programming (Python) › Nested data structures — list of dicts, dict of lists
Nested structures ko chhune se pehle tumhe un pieces mein fluent hona chahiye jinse woh bane hain. Yeh page un sabhi pieces ko scratch se build karta hai — ek samajhdaar 12-saal-ka baccha jisne sirf x = 5 jaisi variable dekhi ho, woh is page ke baad students[0]["name"] ko zor se padh sake aur exactly jaane ki kya hota hai.
1. Ek value — atom
Picture: ek value blueprint par ek dot hai. Abhi kuch bhi nested nahi hai.
Yeh topic iske kyun zaroorat hai: humare data table ki har cell (ek naam, ek age, ek mark) ek value hai. Lists aur dicts bas values ke containers hain.
2. List — numbered boxes ki ek ordered row

Figure dekho: boxes ke neeche 0, 1, 2 number hain, cyan mein. Value 88 index 0 par rehti hai, 73 index 1 par, 95 index 2 par.
Yeh topic iske kyun zaroorat hai: list-of-dicts ki rows, aur dict-of-lists ki column strips, dono lists hain. Isse bachna possible nahi.
Link: Lists.
3. Square-bracket indexing — "box kholo, slot lo" operation
Picture: ek amber arrow box 1 mein ghusta hai aur 73 nikalke laata hai. Woh arrow hi [...] hai.
Yeh topic iske kyun zaroorat hai: parent note mein har access — students[0], students["name"], students[0]["name"] — ek ya zyada in bracket dives ka, chained hota hai. Ek dive master karo aur chain easy ho jaati hai.
4. Dict — numbered ki jagah labelled boxes

Figure mein boxes par word labels hain (name, age, marks) number ki jagah. Bas yahi dict aur list mein poora fark hai.
Yeh topic iske kyun zaroorat hai: list-of-dicts mein, har row ek dict hai; dict-of-lists mein, outer bundle ek dict hai jiske keys column names hain. Is topic ka aadha hissa dicts ka hai.
Link: Dictionaries.
5. Dict indexing — same brackets, lekin label ek key hai
Yeh topic iske kyun zaroorat hai: parent ka core insight "pehle outer container peel karo, phir inner" poori tarah is baat par based hai ki tumhare haath mein jo box hai woh list hai (number chahti hai) ya dict (key chahti hai).
6. Nesting — ek value khud ek container ho sakti hai

Figure dono ko box-in-a-box ke roop mein dikhata hai: left par ek outer numbered box jiske slots mein labelled dicts hain; right par ek outer labelled box jiske slots mein numbered lists hain.
7. Chained brackets — do baar dive karo
Yahi reason hai ki parent note cells ko students[i][c] (LoD) vs students[c][i] (DoL) likhta hai: jo container bahar hai woh decide karta hai ki kaunsa label pehle aata hai.
8. len() — kitne boxes hain?
Yeh topic iske kyun zaroorat hai: marks ka average nikalne ke liye rows ki count se divide karte hain — woh number len(students) (LoD) ya len(students["marks"]) (DoL) hai.
9. for — har box ko baari baari visit karo
Yeh topic iske kyun zaroorat hai: list-of-dicts se column nikalna (for row in lod) aur dict-of-lists se rows banana dono for par depend karte hain.
Link: Loops and Iteration.
10. Comprehension — ek loop ek line mein compress
Yeh topic iske kyun zaroorat hai: parent ki transpose one-liner {k: [row[k] for row in lod] for k in lod[0]} do nested comprehensions hain. Is piece ke bina tum ise padh nahi sakte.
Link: List Comprehensions.
11. Mutable — boxes jo tum in place edit kar sakte ho
Picture: do dict keys se do amber arrows ek hi single box par utarte hain. Box change karo, dono arrows wahi change dekhenge.
Link: Mutable vs Immutable.
Prerequisite map
Equipment checklist
Kaun sa symbol list banata hai, aur kaun sa dict?
[ ] (square brackets) list banate hain; { } (curly braces) dict banate hain.Kisi bhi list ka pehla index kya hota hai?
0 — tum front door se aage boxes count karte ho, aur pehla 0 steps aage hota hai.container[k] mein, k kab number hoga aur kab key?
container list ho; key (usually string) tab jab woh dict ho. Pehle outer container ka type check karo.List par marks["age"] se kaun sa error aata hai?
TypeError — list sirf integer positions accept karti hai.String-keyed dict par asha[0] se kaun sa error aata hai?
KeyError — dict sirf apni keys accept karta hai.lod[0]["name"] mein [0] pehle kyun aana chahiye ["name"] se?
lod ek list hai, toh [0] pehle use dict mein turn karta hai; tabhi key "name" kaam karti hai.List of dicts mein len(students) kya count karta hai?
List comprehension [m+5 for m in xs] kya produce karta hai?
xs mein har m ke liye m+5 ke saath ek bilkul nayi list.dict.fromkeys(cols, []) se shared-list bug kyun hota hai?
Connections
- Lists — §2 mein bana numbered-box container
- Dictionaries — §4 mein bana labelled-box container
- List Comprehensions — §10 ka one-line loop
- Loops and Iteration — §9 ka
for - Mutable vs Immutable — §11 ka shared-box hazard
- JSON and APIs — jahan se real lists-of-dicts aate hain
- Pandas DataFrame — professional dict-of-lists
- Parent topic