GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Dictionaries (with Examples) - GeeksforGeeks Python DictionaryLast Updated : 19 Sep, 2026 Dictionary is a data structure that stores information in key-value pairs. While keys must be unique and immutable (like strings or numbers), values can be of any data type, whether mutable or immutable. This makes dictionaries ideal for accessing data by a specific name rather than a numeric position like in list. Example: This example shows how a dictionary stores data using keys and values.
Output {'name': 'Jake', 'age': 22}
Explanation: "name" and "age" are keys, "Jake" and 22 are their values and dictionary stores data in key:value format Creating a DictionaryA dictionary is created by writing key-value pairs inside { }, where each key is connected to a value using colon (:). A dictionary can also be created using dict() function.
Output {'x': 1, 'y': 2}
{'name': 'Sam', 'age': 20}
Accessing Dictionary ItemsA value in a dictionary is accessed by using its key. This can be done either with square brackets [ ] or with the get() method. Both return the value linked to the given key.
Output Kat 21
Adding and Updating Dictionary ItemsNew items are added to a dictionary using the assignment operator (=) by giving a new key a value. If an existing key is used with the assignment operator, its value is updated with the new one.
Output {'name': 'Alex', 'age': 21}
Removing Dictionary ItemsDictionary items can be removed using built-in deletion methods that work on keys: 1. del: del removes an item using its key
Output {'b': 2}
2. pop(): pop() removes the item with the given key and returns its value
Output 1
{'b': 2}
3. popitem(): popitem() removes and returns the last inserted key-value pair
Output ('b', 2)
4. clear(): clear() removes all items from the dictionary
Output {}
Iterating Through a DictionaryA dictionary can be traversed using a for loop to access its keys, values or both key-value pairs by using the built-in methods keys(), values() and items(). 1. Iterate keys: Returns all keys from the dictionary.
Output a b 2. Iterate values: Returns all values from the dictionary.
Output 1 2 3. Iterate key-value pairs: Returns all key-value pairs as tuples.
Output a 1 b 2
Nested DictionariesA nested dictionary is a dictionary that contains another dictionary as one of its values. Below diagram shows how a nested dictionary works, where key 3 points to another dictionary inside the main dictionary. ![]() The arrows show how each key is connected to its corresponding value.
Output Sam Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |