SECTION 03 / 31

A key is a label, not a list position

1 min read
marks[21] looks for the CardNo 21 key and returns 74. There are only two entries in this miniature dictionary.
marks[21] looks for the CardNo 21 key and returns 74. There are only two entries in this miniature dictionary.

The dictionary {7:76,21:74} has two entries. Yet marks[21] is a valid lookup because 21 is a key. It does not request the 21st element. Similarly, CardNo 0 is a real student ID in the dataset; it does not mean “missing”.

Choose a unique key when you want one entry per student. Names can repeat. Two distinct students called Asha would collide in a name → mark dictionary. The later assignment would overwrite the earlier mark. IDs 101 and 202 avoid that problem in the constructed example below.

REAL COURSE DATA · KEYS, COUNTS AND CONNECTIONS

Why names can overwrite another record

Follow the dictionary as its keys and values change. Predict each lookup or update before revealing the next step.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next key, whether it already exists, and the value that will be stored.

Different keys are allowed to store the same value. Rida and Bhuvanesh both have Chemistry 78, but their CardNo keys 3 and 0 are different. Unique KEYS do not imply unique VALUES.

Try it yourself

Does marks[21] require at least 21 dictionary entries?

Show explained answer

No. It requires the key 21 to exist.

Try it yourself

Can {101:70,202:70} be valid?

Show explained answer

Yes. The keys differ; equal values are allowed.

Try it yourself

Why can names lose information?

Show explained answer

If two records share a name, assigning by that name replaces the first value. Use unique IDs or deliberately group multiple IDs under a name.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑