I am importing a list from an outside document, and inputing that list into a dictionary. This question is applicable to pretty much all values associated to a variable from inside a function. Once the function is complete how do I pull that information from the function with out having to assign the variables as global. Sorry if this question isnt very clear, im having trouble vocalizing it.
Here is the program so far. the dictionary ‘result’ has values while in the function but when I try to call it from outside the function its empty.
fin = open('words.txt')
def dictionary(words):
result = {}
for line in words:
result[line] = 'yupp!' # dont care about value at the moment
return result
word_dict = dictionary(fin)
'the' in word_dict# checking to see if a word is in the dictionary
Use:
to assign the value returned by
dictionaryto the variableresult.Note that
resultis a global variable, so I’m not sure what you mean by “without having to assign the variables as global”.
Alternatively,