In this lesson we want to talk about Python Dictionary Methods.
What is Python Dictionary ?
Python dictionary is collection of key value pairs, where each key is mapped to specific value. Dictionaries are also known as associative arrays or maps in other programming languages.
In Python, dictionaries are defined using curly braces {}
and are composed of key value pairs separated by colons :
. for example:
1 |
person = {'name': 'John', 'age': 32, 'gender': 'male'} |
In this example, 'name'
, 'age'
, and 'gender'
are the keys, and 'John'
, 32
, and 'male'
are the values associated with those keys.
Dictionaries are unordered and mutable, meaning that you can add, remove and modify elements in a dictionary after it has been created. they are also optimized for fast lookups, making them useful for data that needs to be quickly retrieved based on specific key.
Learn More on Python
- How to Convert Integer to Roman in Python
- How to Sort Words in Python
- Python Best Libraries for Web Development
- Top 10 Python REST API Frameworks
- Python Random Module
- Build Python REST API with FastAPI
Python Dictionary Methods
Python dictionaries have several built in methods that allow you to perform common operations on dictionaries. some of the most commonly used dictionary methods include:
dict.clear()
: Removes all items from the dictionary.dict.copy()
: Returns a shallow copy of the dictionary.dict.fromkeys(keys, value)
: Returns a new dictionary with the specifiedkeys
and each key mapped to the specifiedvalue
.dict.get(key, default)
: Returns value associated with the specifiedkey
or thedefault
value if the key is not found.dict.items()
: Returns view of the dictionary’s (key, value) pairs as a list of tuple.dict.keys()
: Returns a view of the dictionary’s keys.dict.pop(key, default)
: Removes specifiedkey
and returns its value or thedefault
value if the key is not found.dict.update(other_dict)
: Adds the items from theother_dict
to the dictionary, overwriting any existing values for matching keys.dict.values()
: Returns a view of the dictionary’s values.
This is an example of using some of the dictionary methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Define a dictionary person = {'name': 'John', 'age': 32, 'gender': 'male'} # Use the items() method to get a view of the dictionary's items items = person.items() print(list(items)) # Use the keys() method to get a view of the dictionary's keys keys = person.keys() print(list(keys)) # Use the values() method to get a view of the dictionary's values values = person.values() print(list(values)) # Use the get() method to retrieve a value for a key age = person.get('age', 0) print(age) |
In this example, we define a dictionary person
with keys 'name'
, 'age'
, and 'gender'
. We then use the items()
, keys()
, and values()
methods to get views of the dictionary’s items, keys, and values, respectively. Finally, we use the get()
method to retrieve the value associated with the 'age'
key.
Run the code and this will be the result
