[Link].
com
Python Dictionaries
1. Introduction to Dictionaries
• A dictionary is a collection that is:
o ordered (unordered as of Python 3.6 and earlier)
o Mutable (changeable)
o Indexed by keys, not numbers
• Dictionaries store data in key-value pairs.
• Keys must be unique and immutable (e.g., strings, numbers, tuples).
• Values can be of any data type.
• Dictionaries are written using curly braces {}.
2. Accessing Dictionary Items
• Use the key to access corresponding value.
• Syntax: dictionary[key]
4. Change Dictionary Items
• Modify value using its key.
• Syntax: dictionary[key] = new value
1
[Link]
5. Add Items to Dictionary
• Add new key-value pair.
• Syntax: dictionary[key] = new value
6. Remove Items
• Use pop(key), popitem() or del.
7. Check if Key Exists
• Use in or not in.
2
[Link]
8. Dictionary Length
• Use len() to count key-value pairs.
9. Dictionary Constructor
• Create dictionary using dict() constructor.
10. Dictionary Methods
• keys(), values(), items(), update(), clear()
3
[Link]
11. Nested Dictionary
• Dictionaries can contain other dictionaries as values.