Kelvin Davis Kelvin Davis - 1 year ago 68
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():
key = input("Please enter a text abbreviation")
for key, value in newDict:

Answer Source

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download