Appending values to a key if key already exists (python/jython)
I have a list that I need to make into a dictionary. The list has duplicate (soon to be) keys which have different values. How do I find these keys and append the new values to it? list=[q:1,w:2,q:7] dictionary= q:1,7 w:2 Thanks in advance Make the values in your dictionary lists, so that you have: dictionary = {'q': [1, 7], 'w': [2] } etc. ie, your one-item values are one-item lists. This means when you have another 'q' , you can do this: dictionary['q'].append(5) Except that dictionary['q'] will be a KeyError the first time you do it, so use setdefault instead: dictionary.setdefault('q', [])