Dictionary

Dictionary

A dictionary is a data structure in programming that stores values under freely chosen keys. You don't look things up by position, but by name — much like in a dictionary, where you look up a word.

Programs need to remember things. There are various ways of storing data in memory for this purpose. One particularly common form is the dictionary. In it, every stored value sits under a name you choose yourself, the key. Instead of saying “give me the third entry,” you say “give me the entry named capital.” The name dictionary comes from the everyday meaning of the word: there, too, you look things up by keyword, not by page number.

Why looking up by name is faster

The obvious alternative is a simple list. To find something in it, you may in the worst case have to go through every entry. With ten entries, that doesn’t matter. With ten million entries, it’s a problem. A dictionary, by contrast, finds the matching entry almost regardless of size — just as fast with ten million entries as with ten.

There’s a second advantage on top of that: readability. Code containing “user['email']” explains itself. Code containing “user[4]” forces every reader to look up what position four means. Large programs consist to a considerable extent of such named accesses.

That’s why the dictionary is built into almost every modern programming language. In Python it’s called dict, in JavaScript every Object behaves this way, in Java there’s the HashMap. The names differ, but the idea is the same everywhere.

The trick behind the hash function

To make lookups this fast, the key is converted into a number. This conversion is handled by what’s called a hash function. It turns the text “capital,” for example, into the number 74. The value is then stored at memory location 74. If the program later searches for “capital” again, it computes the same number and goes directly to that spot. It doesn’t need to search through anything.

This only works if the hash function always produces the same result. However, two different keys can end up landing on the same number by chance. This is called a collision. In that case, the dictionary stores both entries at that location and briefly checks during lookup which one was meant. Such cases are rare enough to barely affect speed.

One important limitation: keys must be unique. If you store something twice under “capital,” the second value overwrites the first. Another common misconception is treating a dictionary as sorted data. The entries have no meaningful order; anyone who needs sorting has to create it themselves.

From the JSON format to AI models

Anyone who has ever looked into a configuration file has seen dictionaries. The JSON format, which programs use to exchange data over the internet, essentially consists of key-value pairs. When an app fetches weather data, a structure like this comes back: temperature, location, timestamp, each under a name.

The term also comes up in AI, though with two meanings. On the one hand, quite practically: settings for a model are passed as a dictionary, as are the trained weights, which are stored in Python libraries as a state dict. On the other hand, in some research papers, dictionary refers to a collection of learned features, for instance in methods that examine which concepts a model distinguishes internally.

For starters, the practical meaning is enough. Anyone learning to program usually uses dictionaries within the first few weeks — for word counts in a text, for user data, or for translation tables. It’s one of the few data structures you need on a daily basis.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.