Kelvin Davis Kelvin Davis - 21 days ago 10
Python Question

Passing a dictionary or its keys as values to a function

So I wrote a function that reads in a .csv file and creates a dictionary of abbreviations and their meaning. Now I'm looking to create a main function that calls the create dictionary function and then prompts the user for keys, but I'm not sure how to access the created dictionary.

import csv

def CreateDictionary(fileName):

with open(fileName, 'r') as f:
reader = csv.reader(f)
newDict = {}
for x, y in reader:
newDict.setdefault(x, []).append(y)
return newDict



def main():
CreateDictionary('textToEnglish.csv')
key = input("Please enter a text abbreviation")
for key, value in newDict:

Answer

You need to assign the return value of the CreateDictionary function to some variable like this:

newDict=CreateDictionary('textToEnglish.csv')

this way you can access the items in the dictionary like this:

newDict["etc"]