Python Tutorial 16: What is Data Dictionary In Python?

Data Dictionary in Python with duplicated keyIn previous tutorial – Python Tutorial 11: Data Type in Python – I have illustrated the following Non-Primitive Data Structure

  • list
  • Set
  • Tuple
  • Data Dictionary

What is Data Dictionary In Python?

Data Dictionary in python is very similar to list that allows multiple items to be stored in the same data set. However, it have

  1. very different way to access items in the data set
  2. Items are not stored in any order
  3. does not have append function

Syntax of  Data Dictionary

Data_Dictionary = { Key 1 : Value 1, Key 2: Value 2, ... key "N", Value "N"}

 

Each item have two parts:

  1. a unique key
  2. a value

Example:

[vtftable cols=”{0}0:ffe599;{/}”]
Patient = {“PAT00001” : “David Cheng”,”PAT00002″:”Alan Joyce”, “PAT00003″:”Florence Lee”};nn;
[/vtftable]

Following is the result

How to define a Data Dictionary in Python

We have created three patients where the key is “PAT00001”, “PAT0002” and “PAT00003”

As mentioned above, the key need to be a unique key. What about if the key is not unique?

Let’s see we try to create a Data dictionary like this:

“Patient = {“PAT00001” : “David Cheng”, “PAT00001” : “David Wong”,”PAT00002″:”Alan Joyce”, “PAT00003″:”Florence Lee”}”

The key PAT00001 have been appeared twice!

Data Dictionary in Python with duplicated key

The first patient is gone from the list! Therefore, Python will not indicate there is a duplicate key, but will simply override the first record by the second record! As a result of this, we have to be very careful while defining the key in  data dictionary. Otherwise, you may lost your data!

 

How to access an item in a data dictionary?

In a list or a tuple we are using the item number to access to the item, Now we are simply to replace the item number by the key.

For example:

Access to a Data Dictionary in Python

What about using a For Loop?

Following is the result

Data_Dict_testing(Patient)
List value for each key
=======================
key —> item
PAT00001 —> David Cheng
PAT00002 —> Alan Joyce
PAT00003 —> Florence Lee

List all Keys only
=======================
PAT00001
PAT00002
PAT00003

List all Items
=======================
(‘PAT00001’, ‘David Cheng’)
(‘PAT00002’, ‘Alan Joyce’)
(‘PAT00003’, ‘Florence Lee’)

 

Data Dictionary.items() returns ll items of a data dictionary.

 

How to update an item in a data dictionary?

It is similar to update an item in a list, but instead of using the item number, we are using the item key.

E.g.

In a list – Patient[0] = “Andy Wong”

Data Dictionary : Patient[“PAT00001”] = “Andy Wong”

 

How to add an item to a data dictionary?

It is much easier than other data type. Simply use a new key.

 

E.G.

Patient [“PAT00004”] = “Nicole Jones”

Then we have added a new patient where the key is PAT0004 in the data dictionary.

How to delete an item to a data dictionary?

E.G. to delete the PAT00004 that we just created from that Patient data dictionary

del Patient[“PAT00004”]

Then the patient “PAT00004” will be deleted.

 

Leave a Comment