Monday, August 6, 2018

(Python) Dictionaries

Having already thoroughly explored the topic of lists within Python, we will now, in a similar manner, explore a data type which is, in many ways, incredibly similar to lists. In a prior entry, I defined Python dictionaries as:

A dictionary is a collection of elements which are unordered. It is modifiable and does not allow duplicate entries.

A Python dictionary is essentially a multi-dimensional list type variable, which, as stated above, cannot contain duplicate entries.

All of this will be elucidated as we work through the following examples.

Single Entry Dictionaries

As mentioned previously, a dictionary variable is very similar to a list variable. We will illustrate this through the creation of a dictionary variable, which can be enabled, through the synthesis of two list variables.

First, let's define each list type variable:

# List 1 #

key = ['key0', 'key1', 'key2', 'key3', 'key4']

# List 2 #

entry = ['entry0', 'entry1', 'entry2', 'entry3', 'entry4']

# Combine lists into a dictionary by utilizing the "dict" and "zip" functions #

dictionaryex = dict(zip(key, entry))

# Print the dictionary variable #


Console Output:

{'key0': 'entry0', 'key1': 'entry1', 'key2': 'entry2', 'key3': 'entry3', 'key4': 'entry4'}

Additionally, we also have the option of manually creating a dictionary variable. The code to achieve this would resemble:

# Manually create the dictionary variable #

dictionaryex = {'key0': 'entry0', 'key1': 'entry1', 'key2': 'entry2', 'key3': 'entry3', 'key4': 'entry4'}

# Print the dictionary variable #


Console Output:

{'key0': 'entry0', 'key1': 'entry1', 'key2': 'entry2', 'key3': 'entry3', 'key4': 'entry4'}

Modifying Dictionary Elements

As was the case with lists, there may be instances in which you desire to add elements, remove elements, or change elements. To code to achieve each is as follows.

# Manually create the dictionary variable #

dictionaryex = {'key0': 'entry0', 'key1': 'entry1', 'key2': 'entry2', 'key3': 'entry3', 'key4': 'entry4'}

# Add element to dictionary variable #

dictionaryex['key5'] = 'entry5'

# Modify element association #

dictionaryex['key0'] = 'en-TREE-0'

# Remove element and associated entry #


# Print dictionary variable to view modifications #


Console Output:

{'key0': 'en-TREE-0', 'key1': 'entry1', 'key3': 'entry3', 'key4': 'entry4', 'key5': 'entry5'}

Calling Specific Dictionary Elements

There may be instance in which you wish to call a specific element in a dictionary without knowing what that element initially is. To identify a dictionary element and have its value returned, you may utilize the following functions.

# Manually create the dictionary variable #

dictionaryex = {'key0': 'entry0', 'key1': 'entry1', 'key2': 'entry2', 'key3': 'entry3', 'key4': 'entry4'}

# Print out the dictionary keys #

print(dictionaryex .keys())

# Print out the entry pertaining to "key2" #


Console Output:

dict_keys(['key0', 'key1', 'key2', 'key3', 'key4'])entry2

You may be wondering why there is an absence of a dictionary feature, the reason for such is due to the nature of the variable, as mentioned previously, "a dictionary is a collection of elements which are unordered", therefore, without this ordering structure, there is no way to call an element by specifically calling its place within the overall order.

Multi-Dimensional Dictionaries

There may arise an occasion when you wish to store multiple entries pertaining to a single key within a dictionary variable. We will explore this phenomenon within the examples below.

# Create multi-dimensional dictionary #

keys = { 'skeletonkey': { 'house': 'hauntedhouse', 'inhabitants':'scary ghosts' },

'carkey': { 'house':'trailer', 'inhabitants':'a man' },

'housekey': { 'house':'manor', 'inhabitants':'empty' }}

# Print out the inhabitants of associated with the skeleton key #


# Add an additional entry #

keys['generickey'] = {'house':'generic', 'inhabitants':'N/A'}

# Print modified dictionary #


Console Output:

scary ghosts
{'skeletonkey': {'house': 'hauntedhouse', 'inhabitants': 'scary ghosts'}, 'carkey': {'house': 'trailer', 'inhabitants': 'a man'}, 'housekey': {'house': 'manor', 'inhabitants': 'empty'}, 'generickey': {'house': 'generic', 'inhabitants': 'N/A'}}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.