Python Dictionary
Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.
- Keys must be a single element
- Value can be any type such as list, tuple, integer, etc.
In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.
Built-in
Dictionary functions
The built-in python dictionary methods along with the description are given
below.
|
SN |
Function |
Description |
|
1 |
cmp(dict1, dict2) |
It compares the items of both the dictionary and returns true if
the first dictionary values are greater than the second dictionary, otherwise
it returns false. |
|
2 |
len(dict) |
It is
used to calculate the length of the dictionary. |
|
3 |
str(dict) |
It converts the dictionary into the printable string
representation. |
|
4 |
type(variable) |
It is
used to print the type of the passed variable. |
Built-in
Dictionary methods
The built-in python dictionary methods along with the description are given
below.
|
SN |
Method |
Description |
|
1 |
dic.clear() |
It is used to delete all the items of the dictionary. |
|
2 |
dict.copy() |
It
returns a shallow copy of the dictionary. |
|
3 |
dict.fromkeys(iterable, value = None, /) |
Create a new dictionary from the iterable with the values equal
to value. |
|
4 |
dict.get(key, default = "None") |
It is
used to get the value specified for the passed key. |
|
5 |
dict.has_key(key) |
It returns true if the dictionary contains the specified key. |
|
6 |
dict.items() |
It
returns all the key-value pairs as a tuple. |
|
7 |
dict.keys() |
It returns all the keys of the dictionary. |
|
8 |
dict.setdefault(key,default= "None") |
It is
used to set the key to the default value if the key is not specified in the
dictionary |
|
9 |
dict.update(dict2) |
It updates the dictionary by adding the key-value pair of dict2
to this dictionary. |
|
10 |
dict.values() |
It
returns all the values of the dictionary. |
|
11 |
len() |
|
|
12 |
popItem() |
|
|
13 |
pop() |
|
|
14 |
count() |
|
|
15 |
index() |
0 Comments