1.2.26 · HinglishIntroduction to Programming (Python)

Nested data structures — list of dicts, dict of lists

1,720 words8 min readRead in English

1.2.26 · Coding › Introduction to Programming (Python)


WHAT are they?

Figure — Nested data structures — list of dicts, dict of lists

HOW do you access data? (derive the indexing from scratch)

Tum hamesha pehle outer container, phir inner ko peel karte ho.

List of dicts — outer ek list hai (integer index use karo), inner ek dict hai (key use karo):

students[0]            # → {"name": "Asha", "age": 20, "marks": 88}   (whole row 0)
students[0]["name"]    # → "Asha"   : list-index 0, THEN dict-key "name"

Ye order kyun? Kyunki students[0] ki value khud ek dict hai, isliye agla [...] ek dict lookup hai, list index nahi.

Dict of lists — outer ek dict hai (key use karo), inner ek list hai (integer index use karo):

students["name"]       # → ["Asha", "Ravi", "Meera"]   (whole column)
students["name"][0]    # → "Asha"   : dict-key "name", THEN list-index 0

WHY pick one over the other?

Task Best structure Reason
Poora record add/remove karna List of dicts ek self-contained dict append karo
Records pe loop karna (for s in ...) List of dicts har item already ek full row hai
Column average / max compute karna Dict of lists column already ek flat list hai
Repeated keys ki memory Dict of lists keys ek baar store hoti hain, har row mein nahi
JSON from an API List of dicts typical JSON arrays of objects se match karta hai

Worked examples


Common mistakes (steel-manned)


Flashcards

List of dicts mein students[2]["age"] ka kya matlab hai?
List index 2 pe dict lo (3rd record), phir uska "age" field padho.
Dict of lists mein same (row 2, age) cell kaise milega?
students["age"][2] — pehle key, phir index. Indices LoD ke mukable swap hote hain.
Column average ke liye dict-of-lists kyun fast hai?
Column already ek flat list hai, isliye sum(...) seedha kaam karta hai — records mein se gather nahi karna padta.
Record add karne ke liye list-of-dicts kyun natural hai?
Har record ek self-contained dict hai jise tum list mein simply append kar sakte ho.
LoD→DoL one-liner?
{k: [row[k] for row in lod] for k in lod[0]}
List of dicts pe students["name"] se kya error aata hai?
TypeError — list ko string key se index nahi kar sakte.
dict.fromkeys(cols, []) se bugs kyun aate hain?
Saari keys EK hi list object share karti hain; ek mutate karo to sab change ho jaati hain. Use karo {c: [] for c in cols}.
LoD aur DoL ka kya relationship hai?
Transposes — same data, rows↔columns; indices order swap karte hain.

Recall Feynman: ek 12-saal ke bache ko samjhao

Socho ek class register. List of dicts aisa hai jaise har student ke liye ek index card ho — har card mein name, age, marks likha ho. Naya student add karna easy hai (naya card) ya ek bacche ka poora card padhna. Dict of lists aisa hai jaise alag-alag columns rakhna: saare naamon ki ek lambi list, saari umar ki ek lambi list, saare marks ki ek list. Sabke marks jodhna easy hai kyunki wo already ek line mein hain. Same bacche, bas sideways store kiye hue hain. Ek fact dhundhne ke liye tum ek card aur ek line choose karte ho — ek mein kehte ho "card 3, marks wala hissa"; doosre mein kehte ho "marks column, 3rd line".

Connections

  • Lists — LoD ka ordered outer container / DoL ka inner
  • Dictionaries — key→value mapping jo dono ko power deta hai
  • List Comprehensions — LoD↔DoL conversion ka engine
  • JSON and APIs — list-of-dicts ka real-world source
  • Pandas DataFrame — ek turbocharged dict-of-lists (columns)
  • Loops and Iteration — nested containers traverse karna
  • Mutable vs Immutable — isliye shared-list bug kaata hai

Concept Map

models

approach A

approach B

transpose of

each item is

each value is

access

access

indices swap

best for

best for

Nested data structures

Spreadsheet rows and columns

List of dicts

Dict of lists

Row record dict

Column list

students[i][c]

students[c][i]

Add records, loop, JSON

Column stats, less key memory