Interview Question

Difference between set and dictionary?

A set stores values; a dictionary maps keys to values.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A set stores unique values, while a dictionary stores key-value pairs. • Set elements must be hashable. • Dictionary keys must be hashable, but values can be any objects. • Both provide fast average-case membership testing in their keys or elements.

💡 Simple Example

numbers = {1, 2, 3} labels = {1: 'one', 2: 'two'} print(2 in numbers, 2 in labels)

Output

True True

⚡ Quick Revision

A set stores values; a dictionary maps keys to values.