Greetings
I'm fairly new to python, and I know that this is possible but I'm having trouble with syntax:
I'm reading a file from the command line and putting it's contents into a dictionary. Keys are repeated in the file, so the values must be placed within a list. In other words, the format for the dictionary must be { Key1 : [Value1, Value2, Value3], Key2 : [ValueX, ValueY], .. etc } rather than { Key1 : Value1, Key1: Value2, Key1: Value3, .. etc}
Both the keys and values are strings. I've tried such operations as
# Line has been split, keyword[1] represents key; keyword[2] represents value
if dict.has_key(keyword[1]):
. . dict[keyword[1]].append(keyword[2])
with little success.
Before inserting the keys and values into the dictionary, I convert the values to a list (containing 1 item). How do I go about appending more objects onto that list once it's placed in the dictionary?
My code segment:
f = open(sys.argv[1])
dict = {}
for line in f:
keyword = line.split()
if keyword[0] == 'PUT': # keyword[1] = key; keyword[2] = value
if dict.has_key(keyword[1]):
# append keyword[2] to keyword[1]'s value list
else:
templist = keyword[2]
list(templist)
dict[keyword[1]] = templist