Python’s Dictionaries

Dictionaries allow us to bind almost limitless amount of real – world data more accurately. It allows you to connect pieces of related information. Like You want to store the information of all the students in a class, so you can make a dictionary representing one student and then store all his/her information as name, age, qualifications etc.

How to Create a Simple Dictionary – As dictionary is a collection of any type of data unlike other data types which have a common element for all its items. Dictionary has a key-value pair (collection). Each key has a associated value. Key makes it easy to retrieve the data.
A dictionary is made by wrapping all the items inside it by curly braces{}. Each item is separated by a comma inside the curly braces and each item represents a key-value pair. A key – value pair is represented as key : value, they are associated to each other so if you want to access value you should now the key. You can store as many items as you want in a dictionary.
Example – Take a student and enter his/her information in dictionary.

student_1 = {'name' : 'Samridhi' , 'nationality' : 'Indian' , 'Stream' : 'Electronics and Telecommunication'}

Here each item is separated by a comma and each key has a value. A key’s value can be anything a number, a list and even another dictionary. While values can repeat but keys are to be immutable and unique.There is also a built-in function dict() to create a dictionary.

Accessing a dictionary To access the value you need a key. In other data types we need some index number to access but in Dictionary we use keys. To get a value you have to mention the dictionary name and the key inside a square bracket or get() method can also be used.
Example – Accessing the name and nationality of student_1

print(student_1['name'])
print(student_1.get('nationality')

Output ->

Samridhi
Indian

The difference while using get() method is that it returns None instead of KeyError , if the key is not found.

Operations on Dictionaries –
1) Add items in Dictionary  – Dictionaries are mutable or dynamic so different changes can be made such as adding an item, changing the value or removing an item. To add a key-value pair, you have to give the name of the dictionary and a key in the square bracket is equal to a value.
Example – Adding the age as a parameter in the student_1 dictionary.

student_1['age'] = 19
print (student_1)

Output – >

{'name' : 'Samridhi' , 'nationality' : 'Indian' , 'Stream' : 'Electronics and Telecommunication' , 'age' : '19'}

If the key would have already present, value gets updated, else a new key: value pair is added to the dictionary. We can also start with an empty dictionary just defining a list with empty braces then start adding the key : value pairs (items).

2) Example of updating a key value in a dictionary –

student_1 = {'age': '19'}
print("The age is" + student_1['age'] + ".")
student_1['age'] = '20'
print("The age is now " + student_1['age'] + ".")

Output ->

The age is 19.
The age is now 20.

Providing the predefined key updates the value of the key.

3) Removing an item from the dictionary – When you think a certain item of information is not required you can simply delete the key:value pair by using the del statement. To delete a key you have to give the dictionary name and the key in the square bracket.
Example – Deleting the Nationality information of student_1

del student_1['nationality']
print(student_1)

Output ->

{'name' : 'Samridhi' , 'Stream' : 'Electronics and Telecommunication' , 'age' : '19'}

The item once deleted by using the del statement can not be retrieved, It is deleted permanently. To store the items but remove from the dictionary pop() method can be used by giving a key value. popitem() method can be used to remove the first key that is stored and clear() method to remove the whole dictionary. del statement can also be used to permanently delete a dictionary.

I will write more about dictionary about looping, storing similar items and how to work on dictionaries in my next blog! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s