Dictionaries are created using curly braces {} to define a collection of key‑value pairs that can be looked up efficiently by their keys. Day to day, this syntax appears in many programming languages and serves as a fundamental building block for storing structured data, configuration settings, or any scenario where a label (the key) maps to a value. Understanding how curly braces work when constructing dictionaries helps you write clearer code, avoid common pitfalls, and use the full power of associative arrays in your projects.
What Is a Dictionary?
A dictionary—also called a map, hash table, or associative array—is a data structure that stores items as pairs:
- Key: a unique identifier used to retrieve the associated value.
- Value: the data linked to the key, which can be any type supported by the language (numbers, strings, objects, even other dictionaries).
Because keys must be unique, looking up a value by its key is typically an O(1) operation on average, making dictionaries ideal for fast look‑ups, counting frequencies, or implementing caches.
The Role of Curly Braces
In most modern languages, the literal syntax for creating a dictionary begins with an opening curly brace {, ends with a closing curly brace }, and separates each key‑value pair with a colon (:). Pairs are usually delimited by commas. For example:
person = {"name": "Ada", "age": 30, "city": "London"}
Here the outer curly braces signal to the interpreter/compiler that what follows is a dictionary literal. Inside, each "name": "Ada" fragment is a key‑value pair where the key "name" maps to the value "Ada" Simple, but easy to overlook..
Why Curly Braces?
Curly braces were chosen historically because they already denoted blocks of code in C‑style languages. Now, reusing them for dictionary literals provides a visual cue that a collection is being defined, similar to how square brackets [] denote lists or arrays and parentheses () denote function calls or tuples. The symmetry of {} also makes it easy to spot the start and end of the literal when scanning code.
Most guides skip this. Don't.
Creating Dictionaries in Popular Languages
Although the core idea is the same, each language has its own nuances regarding allowed key types, mutability, and additional features. Below are examples of dictionary creation using curly braces in several widely used languages.
Python
Python’s built‑in dict type uses curly braces for literals. Keys must be immutable (e.g., strings, numbers, tuples).
# Empty dictionary
empty = {}
# Non‑empty dictionary
capitals = {
"France": "Paris",
"Japan": "Tokyo",
"Brazil": "Brasília"
}
Note: A trailing comma after the last pair is allowed and often encouraged for easier diff‑friendly version control Simple as that..
JavaScript
In JavaScript, object literals—created with curly braces—serve as dictionaries. Keys are strings (or symbols) and can be written without quotes if they are valid identifiers And it works..
const user = {
id: 42,
name: "Ada Lovelace",
isActive: true
};
// Accessing values
console.Day to day, log(user["name"]); // "Ada Lovelace"
console. log.user.
JavaScript objects also support computed property names using brackets inside the braces:
```javascript
const key = "email";
const contact = {
name: "Ada",
[key]: "ada@example.com"
};
Java (with Map Implementations)
Java does not have a literal syntax for maps, but you can create an immutable map using Map.of() (Java 9+) or a mutable one with constructors. Curly braces appear when using anonymous inner classes or double‑brace initialization (though the latter is discouraged) No workaround needed..
Real talk — this step gets skipped all the time.
import java.util.Map;
// Immutable map (Java 9+)
Map scores = Map.of(
"Alice", 85,
"Bob", 90,
"Cara", 78
);
// Mutable map using HashMap
Map mutableScores = new HashMap<>();
mutableScores.put("Alice", 85);
mutableScores.put("Bob", 90);
While Java lacks a direct brace‑based literal, the concept of pairing keys with values inside braces is still present in the Map.of call Still holds up..
C#
C# offers collection initializers that resemble curly‑brace syntax for dictionaries:
var capitals = new Dictionary
{
{"France", "Paris"},
{"Japan", "Tokyo"},
{"Brazil", "Brasília"}
};
The outer new Dictionary<string, string> { … } uses braces to denote the initializer list, and each pair is wrapped in its own braces It's one of those things that adds up..
Ruby
Ruby’s hash literals also use curly braces, with the => operator separating keys and values:
person = { name: "Ada", age: 30, city: "London" }
# Using the newer symbol‑key syntax (Ruby 1.9+)
PHP
PHP arrays can act as dictionaries when using string keys. Worth adding: curly braces are not used for literals; instead, the array() construct or short [] syntax is used. Even so, when defining object properties, braces appear in the class definition.
$capitals = [
"France" => "Paris",
"Japan" => "Tokyo",
"Brazil" => "Brasília"
];
Although PHP does not use braces for dictionary literals, many developers still think of the associative array as a “dictionary‑like” structure.
How Braces Translate to Internal Representation
When the parser encounters an opening curly brace that follows a dictionary‑specific context (e.g., after dict in Python, or as part of an object literal in JavaScript), it expects a series of key‑value tokens until it sees the matching closing brace.
Not the most exciting part, but easily the most useful.
- Tokenization – The source code is broken into tokens:
{,"name",:,"Ada",,,"age",:,30,}, etc. - Parsing – The parser builds an abstract syntax tree (AST) node representing a dictionary literal. Each pair becomes a child node with a key subtree and a value subtree.
- Code Generation / Interpretation – At runtime (or compile‑time for static languages), the interpreter allocates a hash table or similar structure, inserts each key after computing its hash, and stores the associated value.
- Optimization – Many languages pre‑allocate the hash table size based on the number of pairs evident in the literal, reducing rehashing overhead.
Understanding this flow explains why duplicate keys are usually overwritten (the
lastkey-value pair in the literal takes precedence. Take this: in Python:
settings = {"theme": "dark", "theme": "light"} # The final value "light" is retained
Python
Python’s dictionary literals are a direct application of curly braces, with keys and values separated by colons:
user = {
"name": "Grace Hopper",
"contribution": "COBOL",
"year_born": 1906
}
Even though Python’s syntax is concise, the underlying mechanism mirrors the steps outlined earlier. The interpreter parses the literal, constructs an internal hash table, and populates it with the provided entries.
Swift
Swift’s dictionary literals also apply curly braces, though the syntax is slightly different due to type inference:
let scores: [String: Int] = [
"Alice": 85,
"Bob": 90,
"Eve": 78
]
Swift’s compiler optimizes dictionary creation by pre-sizing the hash table when possible, similar to other languages discussed Less friction, more output..
Go
Go uses a more verbose syntax for map literals, but the principle remains the same:
ages := map[string]int{
"Alice": 30,
"Bob": 25,
}
Go’s runtime handles hash collisions and resizing dynamically, though the literal syntax allows the compiler to pre-allocate space if the number of entries is known at compile time Which is the point..
Conclusion
Across programming languages, the humble curly brace serves as a bridge between human-readable syntax and machine-level data structures. Because of that, this syntactic similarity not only aids developers in reading and writing code but also reflects a shared computational foundation: the hash table. Whether in JavaScript’s object literals, Python’s dictionaries, or Swift’s type-annotated collections, the pattern of pairing keys with values within braces remains consistent. By understanding how these literals are parsed and transformed into efficient in-memory structures, programmers can write more idiomatic and performant code, leveraging the language’s built-in optimizations while maintaining clarity and conciseness Practical, not theoretical..