A Python dictionary is a built-in data structure that allows us to store data in key-value pairs. And each key must be unique and associates with a value.
From Python 3.6. The built-in dict class now keeps its items ordered as well.
Below are some important key properties and characteristics of Python dictionaries:
- Key-Value Pairs: Python stores data in dictionaries in the form of key-value pairs, with each key associated with a value.
- Mutable: Dictionaries are mutable, meaning we can modify them after creation by adding, updating, or removing key-value pairs.
- Unique Keys: Each key in a dictionary must be unique. No two keys can be the same within the same dictionary.
- Flexible Data Types: Both keys and values in a dictionary can be of any data type, including strings, numbers, lists, tuples, or even other dictionaries.
- Accessing by Key: You access values in a dictionary by their corresponding keys rather than by index, providing efficient lookup.
- Variable Size: Dictionaries can dynamically grow or shrink in size as you add or remove key-value pairs.
- Heterogeneous Elements: python dictionary can contain a mix of different types of keys and values within the same dictionary. like ex_dict = {“key1”: [], “key2”: {} }
- Optimized for Lookups: Dictionaries offer fast access to values based on keys, making them suitable for scenarios requiring quick data retrieval based on known identifiers(keys).
Python Dictionary Built-In Methods With Examples
clear()
- Property: Removes all key-value pairs from the dictionary.
- Constraint: It does not accept any parameters.
- Use Case: Useful when we want to reset or clear the contents of a dictionary, making it empty or wants to make dictionary empty after job done.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} indians.clear() print(indians) # Output: {}
copy()
- Property: Returns a shallow copy of the dictionary.
- Constraint: It does not accept any parameters.
- Use Case: When we need a copy of a dictionary for manipulation without altering the original dictionary.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} school = indians.copy() print(school) # Output: {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000}
get(key[, default]
- Property: Returns the value for a given key, or a default value if the key is not present.
- Constraint: Key is mandatory, default is optional.
- Use Case: Ideal for safely retrieving values from a dictionary without risking KeyError.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} population = indians.get('Mumbai') print(population) population = indians.get('Indore', "5000") print(population) population = indians.get('Indore') print(population) # Output: # 10000000 # 5000 # None
items()
- Property: Returns a view object containing key-value pairs as tuples.
- Constraint: It does not accept any parameters.
- Use Case: Helpful when we need to iterate over both keys and values in a dictionary.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} items = indians.items() print(items) # Output: dict_items([('Mumbai', 10000000), ('Delhi', 15000000), ('Bangalore', 8000000)])
keys()
- Property: Returns a view object containing all keys in the dictionary.
- Constraint: It does not accept any parameters.
- Use Case: Useful when we need to iterate over keys or check for the existence of specific keys.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} cities = indians.keys() print(cities) # Output: dict_keys(['Mumbai', 'Delhi', 'Bangalore'])
values()
- Property: Returns a view object containing all values in the dictionary.
- Constraint: It does not accept any parameters.
- Use Case: Useful when we need to iterate over values or check for the existence of specific values.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} populations = indians.values() print(populations) # Output: dict_values([10000000, 15000000, 8000000])
pop(key[, default])
- Property: Removes and returns the value for a given key, or a default value if the key is not present.
- Constraint: Key is mandatory, default is optional.
- Use Case: Handy for removing specific items from a dictionary while also retrieving their values.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} population = indians.pop('Delhi') print(population) # Output: 15000000
popitem()
- Property: Removes and returns the last key-value pair as a tuple.
- Constraint: It does not accept any parameters.
- Use Case: Useful when you need to retrieve and remove an arbitrary item from the dictionary.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} item = indians.popitem() print(item) # Output: ('Bangalore', 8000000)
update(iterable)
- Property: Updates the dictionary with key-value pairs from another dictionary or iterable.
- Constraint: Accepts a dictionary, another iterable, or keyword arguments.
- Use Case: Useful for merging dictionaries or updating a dictionary with new key-value pairs.
indians = {'Mumbai': 10000000, 'Delhi': 15000000} new_population = {'Bangalore': 8000000} indians.update(new_population) print(indians) # Output: {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000}
len()
- Property: Returns the number of key-value pairs in the dictionary.
- Constraint: It does not accept any parameters.
- Use Case: Handy for checking the size or length of a dictionary.
indians = {'Mumbai': 10000000, 'Delhi': 15000000, 'Bangalore': 8000000} count = len(indians) print(count) # Output: 3