0% found this document useful (0 votes)
50 views

Creating A Dictionary

To create a Python dictionary, items are passed inside curly braces {} with keys and values separated by colons. Keys must be unique, but values can be of any data type and repeat. Dictionaries can be created empty, from sequences, or by calling the dict() method. Values can be accessed, updated, or deleted using keys and various methods like pop(), popitem(), and del. The entire dictionary can be deleted using del or cleared of elements using clear().

Uploaded by

Shilpita Jana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Creating A Dictionary

To create a Python dictionary, items are passed inside curly braces {} with keys and values separated by colons. Keys must be unique, but values can be of any data type and repeat. Dictionaries can be created empty, from sequences, or by calling the dict() method. Values can be accessed, updated, or deleted using keys and various methods like pop(), popitem(), and del. The entire dictionary can be deleted using del or cleared of elements using clear().

Uploaded by

Shilpita Jana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

The following examples are demonstrate how to create the Python


dictionaries:

Creating an empty dictionary:


dict_sample = {}
print(dict_sample)

Output:
{}
Creating a dictionary with integer keys:
dict_sample = {1: 'mango', 2: 'pawpaw'}
print(dict_sample)

Output:
{1: 'mango', 2: 'pawpaw'}

Creating a dictionary with mixed keys:


dict_sample = {'fruit': 'mango', 1: [4, 6, 8]}
print(dict_sample)

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'}

A dictionary can also be created from a sequence as shown below: Dictionaries


can also be nested, which means that we can create a dictionary inside another
dictionary.

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

We created a dictionary named dict_sample. A variable named x is then created


and its value is set to be the value for the key "model" in the dictionary.
UPDATING DICTIONARY:
After adding a value to a dictionary we can then modify the existing dictionary
element. You use the key of the element to change the corresponding value.

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

You might also like