Moderate Data Types Question 71 of 228

Why must dictionary keys be hashable, and what does that imply for lists?

Python & Django · Speak this in 60–90 seconds · Faridabad & Delhi NCR

PICTURE THIS: PYTHON TYPES

intfloatstrboollisttupledictset

Simple meaning

Dicts locate entries by hash then equality, so a key must implement a stable __hash__ and __eq__.

1

WHY — Data Types instead of guessing?

Why interviewers care about Data Types:

They are checking judgment

on Data Types.

A good answer names

the situation, the default choice, and one exception - that reads as experience.

Stay structured

Name the idea, why it exists, then one short example.

Close cleanly

End with when you use it and one common pitfall.

2

STEPS — What happens step by step?

Before you speak the answer, walk the interviewer through these steps:

  1. 1
    Dicts locate entries by

    hash then equality, so a key must implement a stable __hash__ and __eq__.

  2. 2
    Lists are mutable, so

    their contents could change after insertion and break the hash table

  3. 3
    How it works

    they are unhashable.

  4. 4
    Tuples of hashables work

    as keys

  5. 5
    tuples that contain a

    list do not.

  6. 6
    Close

    When you pick this over the alternative.

3

EXAMPLE — See it in action

Here's a short line you can speak, broken into clear beats:

Say this line
“Lists are mutable, so their contents could change after insertion and break the ”
Break into beats
Listsaremutablesotheircontents
Speaking order
2987408337471632900

Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.

Key takeaway

Dicts locate entries by hash then equality, so a key must implement a stable __hash__ and __eq__. Lists are mutable, so their contents could change after insertion and break the hash table

Chat with us