Reputation: 39
If have to following dict:
graph = {'A': ['B', 'C'], 'B': ['C', 'D']}
How can i add an extra value to (for example) key a (so the result will be
graph = {'A': ['B', 'C', 'Z'], 'B': ['C', 'D']}
Upvotes: 1
Views: 9855
Reputation: 12077
Your values in the dictionary are lists. Thus the following works:
>>> graph['A'].append('Z')
list.append()
adds an item to the end of the list.
Example:
>>> graph = {'A': ['B', 'C'], 'B': ['C', 'D']}
>>> graph['A'].append('Z')
>>> graph
{'A': ['B', 'C', 'Z'], 'B': ['C', 'D']}
Upvotes: 7