What is a dictionary in programming?
In short
A dictionary stores pairs of keys and values, so you can look a value up by its name instead of its position.
A real dictionary lets you look up a word to find its meaning. A dictionary in code works the same way: you look up a key, like "name" or a username, to find its value. It's perfect for things like a phone book, a user's settings or how many times each word appears in a text.
Python calls them dictionaries. In JavaScript you'd use an object or a Map, and other languages call them hash maps.
Python
Look up, change and add values by key
ages = {"Ada": 36, "Alan": 41}
print(ages["Ada"])
ages["Grace"] = 85
ages["Ada"] = 37
print(ages)Output
36
{'Ada': 37, 'Alan': 41, 'Grace': 85}JavaScript
The same idea in JavaScript, with an object
const ages = { Ada: 36, Alan: 41 };
console.log(ages["Ada"]);
ages.Grace = 85;
ages.Ada = 37;
console.log(ages);Output
36
{ Ada: 37, Alan: 41, Grace: 85 }Learn it properly
These free lessons teach it step by step, with questions and a program you write yourself.
Try your first lesson free
Codiqo teaches coding in five-minute lessons with real code. Free on the web, iPhone and Android.