In this article Dictionary Functions in Python we give the information about many built-in functions in python such as len(), types(), sorted(), etc.
Dictionary Functions in Python:
Built-In Dictionary Functions and Methods:
There are many built-in functions and methods available for Dictionary in Python, which are used to simplify operations with the dictionary.
1. Built-In Functions:
(a)len()
Usage: Gets the number of key-value pairs present in the dictionary.
Example
my_dict = {“a”: 1, “b”: 2, “c”: 3}
print(len(my_dict))
# Output: 3
(b) types()
Usage: Identifies the data type of Dictionary.
Example:
my_dict = {“a”: 1}print(type(my_dict))
# Output: <class ‘dict’>
(c) sorted()
Usage: Returns the keys of the dictionary in sorted order as a list.
Example:
my_dict = {“b”: 2, “a”: 1, “c”: 3}
print(sorted(my_dict))
# Output: [‘a’, ‘b’, ‘c’]
2. Dictionary Methods:
(a) dict.keys()
Usage: Returns all the keys of the dictionary in a list-like object.
Example
my_dict = {“a”: 1, “b”: 2}
print(my_dict.keys())
# Output: dict_keys([‘a’, ‘b’])
(b) dict.values()
Usage: Returns all the values of the Dictionary in a list-like object.
Example
my_dict = {“a”: 1, “b”: 2}
print(my_dict.values())
# Output: dict_values([1, 2])
(c) dict.items()
Usage: Returns the key-value pairs in the dictionary as a tuple.
Example:
my_dict = {“a”: 1, “b”: 2}
print(my_dict.items())
# Output: dict_items([(‘a’, 1), (‘b’, 2)])
(d) dict.get(key, default)
Usage: Returns the value of the given key; Returns the default value if the key does not exist.
Example
my_dict = {“a”: 1}print(my_dict.get(“a”))
# Output: 1print(my_dict.get(“b”, 0))
# Output: 0
(e) dict.update()
Usage: Adds new key-value pairs to the dictionary or updates the values of existing keys.
Example
my_dict = {“a”: 1}my_dict.update({“b”: 2, “a”: 3})
print(my_dict)
# Output: {‘a’: 3, ‘b’: 2}
(f) dict.pop(key, default)
Usage: Deletes the given key and returns its value; Returns the default value if the key does not exist.
Example
my_dict = {“a”: 1, “b”: 2}
print(my_dict.pop(“a”))
# Output: 1
print(my_dict)
# Output: {‘b’: 2}
(g) dict.popitem()
Usage: Removes the last key-value pair from the Dictionary and returns it as a tuple.
Example:
my_dict = {“a”: 1, “b”: 2}
print(my_dict.popitem())
# Output: (‘b’, 2)
(h) dict.clear()
Usage: Removes all items from the Dictionary.
Example:
my_dict = {“a”: 1, “b”: 2}
my_dict.clear()
print(my_dict)
# Output: {}
(i) dict.copy()
Usage: Creates a shallow copy of the Dictionary.
Example:
my_dict = {“a”: 1}
new_dict = my_dict.copy()
print(new_dict)
# Output: {‘a’: 1}
(j) dict.setdefault(key, default)
Usage: Returns the value of the given key; If the key does not exist then associates the key with the default value.
Example:
print(my_dict.setdefault(“b”, 2))
# Output: 2
print(my_dict)
# Output: {‘a’: 1, ‘b’: 2}
Example:
# use of Dictionary
my_dict = {“name”: “Rahul”, “age”: 25, “city”: “Delhi”}
# Keys or Values
print(my_dict.keys())
# Output: dict_keys([‘name’, ‘age’, ‘city’])
print(my_dict.values())
# Output: dict_values([‘Rahul’, 25, ‘Delhi’])
# Items
print(my_dict.items())
# Output:
dict_items([(‘name’, ‘Rahul’), (‘age’, 25), (‘city’, ‘Delhi’)])
# Get or Update
print(my_dict.get(“name”))
# Output:
Rahulmy_dict.update({“age”: 26})
print(my_dict)
# Output: {‘name’: ‘Rahul’, ‘age’: 26, ‘city’: ‘Delhi’}
# Pop or Clear
my_dict.pop(“city”)
print(my_dict)
# Output: {‘name’: ‘Rahul’, ‘age’: 26}
my_dict.clear()
print(my_dict)
# Output: {}
Properties of Dictionary Keys:
Immutable:
Dictionary keys should be immutable. This means that you can use as keys only objects that cannot be changed, such as strings, numbers, or tuples (if the elements inside the tuple are also immutable).
Example:
my_dict = {42: “Answer”, “name”: “Python”}
But lists or other mutable objects cannot be used as keys:
my_dict = {[1, 2, 3]: “Invalid”} # this will give error
Unique:
Dictionary keys are unique. If you use the same key twice, the last value will be considered.
Example
my_dict = {“a”: 1, “b”: 2, “a”: 3}
print(my_dict)
# Output: {‘a’: 3, ‘b’: 2}
Hashable:
Keys must be hashable. This means that the key must have a stable hash value, which helps organize the data structure efficiently. Immutable objects such as integers, strings, and tuples are hashable.
Example
my_dict = {(“x”, “y”): “Coordinate”}
print(my_dict)
# Output: {(‘x’, ‘y’): ‘Coordinate’}
Data Types of Keys:
Various types of data can be used for keys, such as: IntegerStringFloatTuple (must be immutable)
Example
my_dict = {1: “Number”, “key”: “String”, 3.14: “Pi”, (1, 2): “Tuple”}
print(my_dict)
# Output: {1: ‘Number’, ‘key’: ‘String’, 3.14: ‘Pi’, (1, 2): ‘Tuple’}
Case-Sensitive:
Dictionary keys are case-sensitive. This means that “Key” and “key” will be considered separate keys.
Example
my_dict = {“Key”: 1, “key”: 2}print(my_dict)
# Output: {‘Key’: 1, ‘key’: 2}
Dynamic Nature:
You can add or remove keys from the dictionary at runtime. This makes the dictionary dynamic.
Example:
my_dict = {“a”: 1}my_dict[“b”] = 2
print(my_dict)
# Output: {‘a’: 1, ‘b’: 2}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining