Dictionaries Data Type and Methods in Python

In this class, we discuss dictionaries data type and methods in python.

For Complete YouTube Video: Click Here

Dictionaries Data Type

Before going to understand Dictionaries Data Type. The reader should have some knowledge of Set dataType. Click here.

Dictionary is changeable, not ordered, and no duplicates. These terms are already explained in previous classes.

Changeable mean mutable. I.e. we can change the elements, add elements, and delete elements.

Dictionary is a key-value pair.

Let’s take an example and understand how key-value pairs look.

a={“Name”:”Suresh”,”age”:35,”DOB”:”25-05-1985″}

In the above example, Name, age, and DOB are keys.

For each Key, we have value. So dictionary is a key-value pair.

Keys should be immutable data types.

Values may take any data type.

Keys are our indexes in the dictionary. We can access values using keys.

We do not have separate indexes in the dictionary.

How is memory allocated to dictionaries?

Python uses the Hash table to allocate memory to dictionaries.

The in-depth understanding of the hash table is done in the Data structures course.

Define and Access Dictionaries

Let’s take examples and understand dictionaries.

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

print(a) will display {“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

Duplicates in Dictionaries

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″,”DOB”:”he”} Here DOB has a duplicate.

Python will consider the latest one. ie last DOB key will be considered.

print(a) will display {“name”:”suresh”,”age”:”35″,”DOB”:”he”}

Mutable as Keys

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″,[1,2,3]:”its a list”}

print(a) will display a error message. unhashable type.

Accessing elements

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

print(a[“name”]) will display suresh. Accessing elements using Key.

Accessing using get method.

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

print(a.get(“name”)) will display suresh.

Change item in dictionaries

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

a[“DOB”]=”26-may-1985″

print(a) will display {“name”:”suresh”,”age”:”35″,”DOB”:”26-may-1985″}

Iterate on dictionaries

a={"name":"suresh","age":"35","DOB":"25-may-1985"}
 for i in a:
     print(i)
     print(a[i])
Output:
name
suresh
age
35
DOB
25-may-1985

The above program loop will iterate on keys.

We can display the value using Key.

To iterate on values. We use the method values.

The program is given below.

a={"name":"suresh","age":"35","DOB":"25-may-1985"}
 for i in a.values():
     print(i)
Output:
suresh
35
25-may-1985

Iterating over items. ie taking both Key and value.

The program to iterate on items is given below. It is using the items method.

a={"name":"suresh","age":"35","DOB":"25-may-1985"}
 for i in a.items():
     print(i)
Output:
('name','suresh')
('age',35)
('DOB','25-may-1985')

Methods in Dictionaries

pop method

The pop method is used to remove an item in the dictionary

Example:

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

a.pop(“DOB”)

print(a) will display {“name”:”suresh”,”age”:”35″}

clear method

The clear method is used to clear the dictionaries.

Example:

a={“name”:”suresh”,”age”:”35″,”DOB”:”25-may-1985″}

a.clear()

print(a) will display {} empty dictionary.

Dictionary inside a dictionary.

a={“names”:{“suresh”:”ece”,”ramesh”:”cse”},”ages”:{“suresh”:25,”ramesh”:35}}

print(a[“names”][“suresh”]) will display ece.

The names key has a dictionary in its value.

So inside the dictionary, it checks for key suresh. and display the value ece.