0

For example I have a dictionary:

dictionary = {'dog' : 'bark', 'cat' : 'kitten'}  

And I want to add another value 'woof' to the key 'dog' so that I get:

{'dog': ['bark','woof'], 'cat' : 'kitten'}  

And then add another value 'speak' to the key 'dog' so that I get:

{'dog': ['bark','woof', 'speak'], 'cat' : 'kitten'}

etc.

Any help is appreciated.

2
  • 6
    When asking a question, it is helpful to include a very specific, answerable question. For example, I do not know exactly what you want me to help you with. It is also best, in situations like these, to show that you have attempted the problem and to describe what problems you have with your attempted solution. If it helps, StackOverflow provides a great guide on how to ask questions. Commented Nov 6, 2017 at 20:38
  • To add to the answers already provided, you may want to first test the key ['dog'] to see if its value is already a list (we know from your example that it's not, but it really doesn't hurt to double-check). You can do this with isinstance. if not isinstance(dictionary['dog'], list): Commented Nov 6, 2017 at 20:58

3 Answers 3

3

You first need to convert the values associated with each key in your dict to lists: You can accomplish this via dict comprehension. Afterwards, it's simply a matter of appending items to the list at the desired key in the dictionary. The following code would work:

dictionary = {"dog" : "bark", "cat" : "kitten"}
dictionary = {key : [value] for key, value in dictionary.items()}

dictionary["dog"].append("woof")
dictionary["dog"].append("speak")

At which point, if you print dictionary, the output will be:

{'dog': ['bark', 'woof', 'speak'], 'cat': ['kitten']}
Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to add that i am looping over it. And coming from that the problem is that every time i create a list out of the values, like in your example, it becomes a list in list.
@HelloThere You shouldn't be looping over the dict comprehension: That should only occur once. It sounds like you may need to move that line outside of the loop.
1

As far as I can tell, the solution is to initialize each dictionary value as a list from the start. For example, initialize your dictionary as dictionary = {'dog' : ['bark'], 'cat' : ['kitten']}. Then, when you want to add new values to your dictionary keys you can use dictionary["dog"].append("speak").

Comments

0

I would implement a defaultdict from the collections module.

Here is an example from the official docs:

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> sorted(d.items())
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.