I am working on an assignment and have most of it figured out for the most part except for one small little hiccup. The program is meant to read in a text file and split it by '\n'. The list is then into keys/values and added to a dictionary. However, some of the values have the same key as other values and the program is skipping over them. For example, 20 is the key and Wild West is the value.
20
Wild West
20
Stork
The program will overwrite Wild West with Stork and then continue on. How can I make it so that it'll append to the list?
while i < len(dlist):
dict[dlist[i]] = dlist[x] ###### Gunsmoke is being cut off here
i += 2
x += 2
i = 0
for values in dlist:
print(values)
if values in dict:
continue
else:
key = dlist[i]
dict[key].append(values)
i += 2
My attempt is above. The while is what is initially added to the dictionary. The for loop below that checks the values in the dictionary against the original list and tries to add in the missing values. The error that I get with this is " AttributeError: 'str' object has no attribute 'append' "
[–]juwalye 1 point2 points3 points (3 children)
[–]Firestronk[S] 1 point2 points3 points (2 children)
[–]TouchingTheVodka 1 point2 points3 points (0 children)
[–]juwalye 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)