# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/dictionary Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/dictionary/en.mdx Output docs content for large language models. --- export const metadata = { title: "Dictionary", description: "Learn dictionary data structure in Python, keys(), values(), items() methods, indexing, for loops, and get() method with practical examples.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/18/2025", subject: "AI Programming", }; ## Definition and Creating Dictionaries Dictionary is a data structure that stores key-value pairs. Think of it like a language dictionary where each word (key) has a meaning (value) associated with it. ### Ways to Create Dictionaries Python provides several ways to create dictionaries, from empty ones to those already containing data. ### Dictionary Syntax Dictionaries are written with curly braces `{}` and contain a list of key-value pairs separated by commas. Each key and value pair is separated by a colon. Dictionaries are enclosed in curly braces, contain a list of key-value pairs separated by commas, and each key-value pair is separated by a colon. ## Dictionary as Container Dictionary belongs to the container category, which are objects that can store collections of other items. Containers differ in how they organize and manipulate the stored items. B[String] A --> C[List] A --> D[Tuple] A --> E[Dictionary] A --> F[Set] E --> E1[Key-value pairs] E --> E2[Unique keys] E --> E3[Mutable] E --> E4[Unordered] `} /> Dictionary has special characteristics as a container. Dictionary stores collections of items in the form of key-value pairs. Containers differ in how items are organized and manipulated. ## Dictionary Characteristics Dictionary has several important characteristics that distinguish it from other data structures in Python. ### Insertion Order and Unique Keys Since Python 3.6, dictionaries maintain the insertion order of elements. When you redefine the value of an existing key, only the latest value is stored without changing the key's insertion order. Keys in dictionaries are unique, only the most recently assigned value will persist. Redefining values does not change the insertion order of keys. ## Accessing Dictionaries Dictionary uses a key-based indexing system to access stored values. ### Indexing with Keys Dictionary uses keys to index values. You can retrieve values by specifying the key in square brackets, change values of existing keys, or add new key-value pairs. ## Dictionary Iteration Dictionary can be iterated in various ways depending on the information you want to access. ### Simple For Loop When you iterate a dictionary directly, Python retrieves keys in their insertion order. ### For Loop with items() Method The `items()` method retrieves key-value pairs simultaneously, allowing you to access both in a single iteration. ## Dictionary Methods Dictionary provides several methods to access keys, values, and key-value pairs. ### keys(), values(), and items() Methods These methods return dynamic view objects. View objects are not lists and cannot be indexed or assigned. Lists can be obtained through the `list()` constructor. View objects maintain reference to the parent dictionary and are efficient for iteration. ## get() Method The `get()` method provides a safe way to access dictionary values without the risk of errors when keys are not found. ### Regular Indexing Problems Indexing a dictionary with a non-existing key will result in a `KeyError`. ### Solution with get() Method The `get()` method retrieves the key's value if it exists, or returns a default value if the key is not found. This prevents the program from stopping due to errors and provides better control in handling incomplete data.