The append function you're referencing only works for lists: https://docs.python.org/2/tutorial/datastructures.html
If you want to add a new key/value pair to a dictionary, use: dictionary['key'] = value
You can also opt for: dictionary.update({'key': value}), which works well for adding multiple key/value pairs at once.
The append function you're referencing only works for lists: https://docs.python.org/2/tutorial/datastructures.html
If you want to add a new key/value pair to a dictionary, use: dictionary['key'] = value
You can also opt for: dictionary.update({'key': value}), which works well for adding multiple key/value pairs at once.
You need to initialize a list rather than just adding the object. Change this:
parents[parent]=kid
to this:
parents[parent] = [kid]
This will give you a list to which you can append() new objects, rather than just a string.
Append values to Dictionary
Guys do not name your variable of type dictionary as myDict/mydict during your whiteboard interviews!
Using Openpyxl how can I write a dictionary into two columns of keys and values?
Appending object to list overwrites previous objects when using dictionary
Videos
I am currently using a while loop to loop and get certain values. I then want to append these values to a dictionary. Every loop through I want to append to two keys
General structure of the code: https://pastebin.com/p4hJcKR5
I have tried using:
dict[key] = value
dict.append(value)
And neither have worked, dict.append gives an error and dict[key] just sets the dictionary to the most recent iteration instead of iterating for all values. Any help would be appreciated.