The Dictionary Data Type

It's time to introduce one of the most important concepts of Python, and one of the last ones to start self exploration in the counteless applications of Python. Dictionary, similar to a list, is a collection of many values. The difference? The indexes for a Dictionary can take up any Data Type, unlike Lists (where it is just integers). These indexes for Dictionaries are called keys that have a value, hence, a key-value pair. This makes a lot of tasks really efficient. Let's discuss the format of a Dictionary first. A Dictionary is initialized using {}. Every Key and value are separated by a colon : and key-value pairs are separated by a comma ,. You can use the python interpretor on the right side (black box on your Repl Window) for the following code:

>>> car = {'color': 'Red', 'name': 'My Car', 'top Speed': '200 mph'}
>>> car
{'color': 'Red', 'name': 'My Car', 'top Speed': '200 mph'}

In the example above, the Dictionary is assigned to car. What are the keys and the values? These pairs are the ones separated by commas. color, name, and top Speed are the keys that have various values. To simplify this further, lets say we have a Dictionary for our name. The keys can be Name, Age, Height, Sex, and so on. The values for these keys can be the value in the key-value pair. To go further with this, what if we want to know the name of the car? How can we find out? Do we have to output the entire Dictionary? Not really, Python is pretty cool!

>>> car['name']
'My Car'

How can we use the values in a statement? Is that possible? Yes, it is!

>>> print('The color of my car is ' + car['color'] +'.')
The color of my car is Red.

Now that we know a little about Dictionaries, we can go on to explain the difference between a Dictionary and a List. These two concepts are quite similar, yet so different.

Dictionaries are unordered, unlike Lists. Hence, they cannot be sliced.

Let's explain this further. With Lists, we have an order, and if another list is not created in the same order, then they can't be similar. Whereas in a Dictionary, if we have two dictionaries that have the same key-value pairs arranged in a different way, they would be considered to be the same. For example:

>>> car = {'color': 'Red', 'name': 'My Car', 'top Speed': '200 mph'}
>>> newCar = { 'name': 'My Car', 'top Speed': '200 mph', 'color': 'Red'}
>>> car == newCar
True

Meanwhile:

>>> car = ['Red', 'My Car', '200 mph']
>>> newCar = ['My Car','Red', '200 mph' ]
>>> car == newCar
False

This difference seems to be small, but can help us in countless ways. Lets consider an example. We own a company with 100 employees and at the end of the month, everyone gets a fixed income. To make this easier, we use a Dictionary, and store everyone's name as the key and income as pair. While filling out the cheque, we can query our system to ask for the employee's income (or value) and it's done! This is how easy things can be while working with Dictionaries.

TRY IT YOURSELF: Finish the given example yourself using code.

results matching ""

    No results matching ""