Creating A Dictionary
Creating A Dictionary
IN PYTHON
Creating a Dictionary:
To create the Python dictionary, we need to pass the sequence of the items
inside curly braces {}, and to separate them using a comma (,). Each item has
a key and a value expressed as an "key:value" pair.
The values can belong to the any of data type and they can repeat, but the keys
are must remain the unique.
Output:
{}
Creating a dictionary with integer keys:
dict_sample = {1: 'mango', 2: 'pawpaw'}
print(dict_sample)
Output:
{1: 'mango', 2: 'pawpaw'}
Output:
{'fruit': 'mango', 1: [4, 6, 8]}
We can also create a dictionary by explicitly calling the Python's dict()
method:
dict_sample = dict({1:'mango', 2:'pawpaw'})
print(dict_sample)
Output:
{1: 'mango', 2: 'pawpaw'}
Example:
dict_sample = {1: {'student1' : 'Nicholas', 'student2' : 'John', 'student3' :
'Mercy'},
2: {'course1' : 'Computer Science', 'course2' : 'Mathematics',
'course3' : 'Accounting'}}
print(dict_sample)
Output:
{1: {'student1': 'Nicholas', 'student2': 'John', 'student3': 'Mercy'}, 2: {'course1':
'Computer Science', 'course2': 'Mathematics', 'course3': 'Accounting'}}
To print the dictionary contents, we can use the Python's print() function and
pass the dictionary name as the argument to the function. For
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
print(dict_sample)
Output:
{'Company': 'Toyota', 'model': 'Premio', 'year': 2012}
ACCESSING VALUES IN A DICTIONARY:
To access the dictionary items, we need to pass the key inside square brackets [].
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
x = dict_sample["model"]
print(x)
Output:
Premio
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
dict_sample["year"] = 2014
print(dict_sample)
Output:
{'Company': 'Toyota', 'model': 'Premio', 'year': 2014}
In this example you can see that we have updated the value for the key "year"
from the old value of 2012 to a new value of 2014.
DELETING ELEMENTS FROM DICTIONARY:
The removal of an element from a dictionary can be done in several ways,
which we'll discuss one-by-one in this section: The del keyword can be used to
remove the element with the specified key.
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
del dict_sample["year"]
print(dict_sample)
Output:
{'Company': 'Toyota', 'model': 'Premio'}
We called the del keyword followed by the dictionary name. Inside the square
brackets that follow the dictionary name, we passed the key of the element we
need to delete from the dictionary, which in this example was "year". The entry
for "year" in the dictionary was then deleted.
Another type to delete a key-value pair is to use the pop() method and pass the
key of the entry to be deleted as the argument to the function.
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
dict_sample.pop("year")
print(dict_sample)
Output:
{'Company': 'Toyota', 'model': 'Premio'}
We invoked that pop() method by appending it with the dictionaryname. And, in
this example the entry for "year" in the dictionary will be deleted.
The popitem() method removes the last item of inserted into the dictionary,
without needing to specify the key. Take a look at the following
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
dict_sample.popitem()
print(dict_sample)
Output:
{'Company': 'Toyota', 'model': 'Premio'}
The last entry into the dictionary was "year". It has been removed after calling
the popitem() function.
But what if you want to delete the entire dictionary? It would be difficult and
cumbersome to use one of these methods on every single key. Instead, you can
use the del keyword to delete the entire dictionary. For
Example:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
del dict_sample
print(dict_sample)
Output:
NameError: name 'dict_sample' is not defined
The code returns an error. The reason is we are trying to access the an
dictionary which is doesn't exist since it is has been deleted.
However, your use-case may require you to just remove all dictionary elements
and be left with an empty dictionary. This can be achieved by calling the clear()
function on the dictionary:
dict_sample = {
"Company": "Toyota",
"model": "Premio",
"year": 2012
}
dict_sample.clear()
print(dict_sample)
The code is returns an empty dictionary since
all the dictionary elements have been removed.
Output:
{}
END