Image for post
Image for post

You will run into Dictionary formatted objects very frequently when writing and reading code in Python. Think of it almost as a real physical dictionary for looking up word definitions, where each word you look up is a unique key and the definition of it is the value of that data. Here is a very useful breakdown on not only what it really is, but how to use and apply it to your everyday coding lifestyle:

What are Dictionaries?

Dictionaries are a built-in Python Data Structure that are mutable. It is similar to Lists, Sets, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. On an abstract level, it consists of a key with an associated value.

What are Keys?

Keys are immutable(this just means that it cannot be changed) data types that can be either strings or numbers. A key cannot however be a mutable data type such as a list. Keys are unique within a dictionary and can not be duplicated inside a dictionary, in case if it is used more than once then subsequent entries will overwrite the previous value.

Image for post
Image for post

Key connects with the value, which creates a map-like structure. If you for a second, remove keys from the picture above, all you are left with is a data structure containing a sequence of numbers. Dictionaries, therefore, hold a key: value pair at each position.

A dictionary is represented by a pair of curly braces {} in which enclosed are the key: value pairs separated by a comma.

Example: dict ={“key_1”: “value_1”, “key_2”: “value_2”, “key_3”: “value_3”}

Unique Keys

Since now you know that keys in a dictionary have to be unique, let's understand it with the help of another example.

Great, so until now, everything looks fine. We were able to print our first dictionary output. Now, let’s repeat the key g with a new value and see what happens.

Image for post
Image for post

As expected, the key g previous value gamma was overwritten by value beta.

Immutable Keys

Now let’s see what happens when we try to define the key as a mutable data type.

Image for post
Image for post

From the above output, we can observe that defining the first key of the dictionary as a list results in a TypeError since dictionary keys must be immutable types and list is a mutable type. There is however a way to work around to this, which is, replacing the list to a tuple, since a tuple is an immutable data type.

Image for post
Image for post

Accessing Key Values

Since we now know how to create dictionaries, let’s learn to access the keys and the values from the dictionary. To access the key value pair, you would use the .items() method, which will return a list of dict_items in the form of a key, value tuple pairs.

Image for post
Image for post

To access keys and values separately, we could use a for loop on the dictionary or the .keys() and .values() method.

Image for post
Image for post

We could also gain access to the keys and values without looping as well.

Image for post
Image for post

We could even access a value by specifying a key as a parameter to the dictionary.

Image for post
Image for post

Congratulations, you now know how to both identify and work with Dictionaries in Python!

RELATED LINKS

Written by

Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store