Making a Python Data Structure

Dictionaries in Python

Making a dictionary in python is easy. It’s a common data structure that you probably learned in an introductory course or a short stackoverflow code snippet. The dictionary data structure looks like this: 

my_dictionary = {‘artist’: ‘owl city’, ‘songname’: ‘hello seattle’}

The following dictionary contains information about an artist (which is an immutable string type) and a songname (which is also an immutable string type). The values are also string types and can be accessed with the usual dictionary[‘key’] notation:

my_dictionary[‘artist’] 

If we print, it would yield this: 

print(myDictionary[‘artist’)
output: owl city

It is best to think of a dictionary as a set of {key: value} pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of {key: value} pairs within the braces adds initial {key: value} pairs to the dictionary; this is also the way dictionaries are written on output [1].

For my project, I want to make a dictionary, but it will contain parameters that the neural network model will need. The keys will be the name of the parameters (mean square deviation, spectral centroid, zero crossing rate) see other blog for more background information [2]. The values will be the result from the calculations from Librosa using the sampling rate and the audio time series. Thankfully Python has a built-in constructor that builds dictionaries from key-value pairs. The syntax for this constructor is:

dict() 

dict() will be applied to a list of tuples: 

dict([(‘key_1’: value_1), (‘key_2’: value_2), (‘key_3’: value_3)])

The dict() constructor will return a dictionary with the following format:

{
‘key_1’: value_1, 
‘key_2’: value_2, 
‘key_3’: value_3
}

Functional Programming

I will use a functional function called make_dictionary() that will take an array of tuples and return the dictionary. 

def make_dictionary(array_tuples):
						return dict(array_tuples)

This will be a functional function because it doesn’t change the array_tuples itself, but instead creates a new data structure while not having to rely on a global variable or other data structure that is on the outside of the function. To make the array of tuples I will use two of these functions. One to make a tuple with the parameter name and its value. The other one to make the dictionary and return it.

def make_tuple(key, value):
    return (key, value)

def make_dictionary(array_tuples):
    return dict(array_tuples)

I can use these new functional functions to construct my new data structure.

tuple_1 = make_tuple(‘spectral_width’, 1)
new_dict = make_dictionary(tuple_1) 

I can then use the new_dict as a data structure that I will be able to feed to the neural network model for it to classify the new input dictionary. I can now return the new_dict and get my data structure with the data that my model will need. 

That was a lot to follow. But the main message that I want to highlight is that you can start with a simple data structure like a list, tuple, or a set and then slowly turn it into a more complex data structure that you can reuse and remake as many times as you like. 

References:

[1] https://docs.python.org/3/tutorial/datastructures.html

[2] https://maryrosecook.com/blog/post/a-practical-introduction-to-functional-programming

Print Friendly, PDF & Email

Published by Mateo Estrada Jorge

Hi, I am a CS and physics double major. I like to rock climb, kayaking, and playing video games. I am always up for a game of chess!

Leave a comment

Your email address will not be published. Required fields are marked *