Working with large dictionaries in python? (performance and crashing)
<p>Goal: to create a python class that will take in a string example 'maple' and translate it into its corresponding species code 'AC'.</p>

<p>Problem: I have created a dictionary that contains all the string inputs and their corresponding species code that will be used for the translation. However I cannot figure out a way to implement the dictionary without python crashing. The dictionary is pretty large (roughly 4000 entries in the format : <code>{'common name example1': 'species code example1', 'common name example2':'species code example2', ... }</code> as shown below. I think my code works in theory, but it is taking up such a large amount of memory that python keeps crashing. </p>

<p>Here is my code:</p>

<pre><code>import string

class exportSpec:

    def __init__(self, exportSpecobj):
        self.set(self.sani(exportSpecobj))
    def get(self):
        return self.exportSpecobj
    def set(self, exportSpecobj_pre_sani):
        self.exportSpecobj = exportSpecobj_pre_sani

    def puncremove(self, common):
        for c in string.punctuation:
        common = common.replace(c,"")
    return common

    def lower(self, common):
        common = self.lower()
        return common

    def codeChange(self, saniCode):
        codeDict = {'balsam fir':'ABBA', 'white fir':'ABCO', 'maple':'AC', 'freeman maple':'ACFR', 'amur maple':'ACGI', 'rocky mountain maple':'ACGL', 'boxelder':'ACNE', 'norway maple':'ACPL', 'red maple':'ACRU', 'silver maple':'ACSA1', 'sugar maple':'ACSA2', 'tatar maple':'ACTA', 'ohio buckeye':'AEGL', 'horsechestnut':'AEHI', 'serviceberry':'AM', 'broadleaf deciduous large':'BDL OTHER', 'broadleaf deciduous medium':'BDM OTHER', 'broadleaf deciduous small':'BDS OTHER'}

# the dictionary^^ is much bigger but I just showed the first 20ish values to show my code

    check = codeDict.has_key(saniCode)
        if check == "True":
            print("New code was found")
            return codeDict[saniCode]
        else:
            print("Error finding code in Dictionary")
            print(saniCode)
            output = 0
            return output

    def sani(self, exportSpecObj):
        if exportSpecObj is None:
            output = 0
            return output
        else:
            exportSpecObjlcpr = self.lower(self.puncremove(exportSpecObj))
            saniCode = codeChange(exportSpecObjlcpr)
            return saniCode
</code></pre>

<p>So I think my code works theoretically. The input comes in and gets sanitized with the methods lower and puncremove to remove punctuation and translate to lower case so that the input can be found in the dictionary to be returned as the value found in the dictionary from the key input. </p>

<p>If there is an easier way to do this without using a dictionary that would work too, but this seemed to be the only way I could think of to accomplish this. The common names and corresponding species codes were originally kept in an excel file and I created the dictionary from that. </p>

<p>I just don't have a lot of experience with coding outside of the classroom and have little to know knowledge about memory and performance of programs. Any help is very much appreciated. Thank you.</p>