Dictionaries – Python

Hello Techies!!

We have learned about lists and tuples till now which were ordered collections and allowed duplicate values.

Now, lets learn more about different type of collections in python. Today, we will look into dictionaries.

As we all know our traditional dictionaries. right?

They are collection of data where we can look for words and find meanings of it.

Similarly, dictionaries in python are collections that are unordered, changeable and does not allow duplicate keys. These are stored in form of key-value pair.

Dictionaries are represented using curly braces {}.

Now, you must be wondering, what does it mean by unordered data. This means that the order in which we are adding elements into our dictionary, it will not remember it. We will have all the data intact but we will lose the order.

For example : Let’s say we add “name” : “piyush” – this means for key name, stored value is piyush.

now we add “designation” : “software developer” – another entry in the dictionary.

general_dictionary = { “name” : “piyush”, “designation” : “software developer” }

When we will print the dictionary, it is not certain that name will come first followed by designation. This is what we meant by order.

DEALING WITH DICTIONARY:

general_dictionary = {“color”:”red”, “animal”:”dog”, “country”:”India”}

ACCESS, CHANGE AND ITERATE ELEMENT FROM DICTIONARY:

dictionaries-1.JPG

In above image, we can see how to define a dictionary by using curly braces and store multiple key-value pairs.

len() function is used to find length of dictionary.

We can access an element same as we access elements in a list or tuple but instead of passing index, here we need to pass key. We can also use in-built get() function to get the same result.

Changing element is also same as changing element in a list or tuple.

Let’s look into iterating through a dictionary.

dictionaries-2.JPG

We have iterated through the dictionary in many ways. First, we used for loop to print out all the keys.

Second, we have iterated through keys using a for loop and print out the values using accessing the element technique.

Third, we have used an inbuilt function – values() of dictionaries to iterate through values directly.

Fourth, we have iterate through dictionary by printing out both key and its corresponding value. We can use items()  function to achieve the same. I think this is best and most useful way of iterating a dictionary.

CHECKING IF ITEM EXISTS IN DICTIONARY, ADDING, DELETING AN ELEMENT :

dictionaries-3.JPG

We can add elements in same way, we add them in list or tuple but here index represents the key.

To remove element one by one, we can use an in-built function popitem(). This will remove last inserted item from dictionary.

We can also use del keyword to remove a specific element or to delete the whole dictionary.

There are many other in-built functions provided by python for using dictionary with more ease.

clear() – This clears all the elements without deleting the dictionary itself.

pop() – This can be used to remove a specific element.

update() – This is used to update the dictionary with the specified key-value pairs.

setdefault() – This returns the value of the specified key. If the key does not exist: insert the key, with the specified value.

This is all that you need to know before starting to use dictionaries.

Don’t forget to practice and play with dictionaries.

See you in the next tutorial.

Happy coding!!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.