0

Sorry if this is a noob question but I am new to programming and python that is why I am asking.

I want to add values to my dictionary keys.

I have dictionary:

dictio = {"Object": "Computer"}

Now within the key "Object", I would like to add the value "Mouse".

So the end result i am looking for is:

>>> dictio
>>> {"Object": ["Computer", "Mouse"]}
5
  • Your dictionary in the result is invalid Commented Sep 25, 2014 at 1:57
  • @karthikr .. what do you mean? Commented Sep 25, 2014 at 2:00
  • Have you tried Google-ing it first before asking? Commented Sep 25, 2014 at 2:00
  • I mean The resulting dictionary should be something like {"Object": ["Computer", "Mouse"]} Try to initialize the dictionary with your resulting dictio, and you will know what i mean Commented Sep 25, 2014 at 2:01
  • oh yes that is what I meant.. the list.. sorry I forgot to add it. Commented Sep 25, 2014 at 2:01

3 Answers 3

4

Your formulation seems to show you didn't grasp correctly what are dict in python. This also makes it difficult for us to understand what you want.

For instance:

I want to add values to my dictionary keys.

Is ambiguous (at least) and could be interpreted in several ways. I'll explain what is ambiguous in the following answer.

Even your original example was not helping, as it is not valid syntax:

>>> {"Object": "Computer", "Mouse"}
SyntaxError: invalid syntax

The vocabulary of dict is about key and values.

In a dict every key has a value and only one.

So here's the question you'll have to answer: What is "Mouse" ? a key or a value of dictio, or none of them ?

Either you wanted:

>>> {"Object": "Computer", "Mouse": "A Sample Value"}

Which is a dictionary with a new pair of key/value. It can be done like this:

>>> dictio["Mouse"] = "A Sample Value"

Or perhaps you wanted to 'add' another value to the already stored value for the key "Object" in the dictionary. But 'adding' a value is ambiguous when speaking of a value in a dict, as dictionaries holds only one value for one key !

  • Do you want the current string value to be concatenated with a new one ?
  • Or do you want to replace the current string value with a list of values ? (If yes, your starting value should have been a list of one element in the first place).

The resulting dict using a list as value for the key "Object" would be:

>>> {"Object": ["My Computer", "Mouse"]}

So this would remain a one key dict, with one value. This value happens to be a list of value, it means that it self, it can hold several inner values in a specific order.

Notice that if I want to start from your original dictio to get the previous result, I have to replace the value "Computer" (of type string) with a different value of different type: ["My Computer", "Mouse"] (it is a list of values).

So this could be done like this:

>>> dictio["Object"] = [dictio["Object"], "Mouse"]

But, this is not very natural and you would probably want to start with a dictio like this:

>>> dictio = {"Object": ["Mouse"]}

Then, 'adding' a value to a list is not anymore ambiguous. And then it would also be simpler to achieve:

 >>> dictio["Object"].append("Mouse") 

I hope reading this helped you to better grasp what are dicts in python. You should probably find tutorials or basic docs about dictionaries as you seem to have missed some fundamental notions.

Sign up to request clarification or add additional context in comments.

Comments

2

One possible approach:

dictio = {"Object": ["Computer"]}

dictio["Object"].append("mouse")

Comments

0

You cannot perform a list operation on a string:

>>> 'string'.append('addition')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

You can only add something to the data structure if the addition is supported:

>>> li=['string']
>>> li.append('addition')
>>> li
['string', 'addition']

You want to take a dict composed of a string associated with another string:

dictio = {"Object": "Computer"}

And add to it as if it were a list. Same problem as above:

>>> dictio["Object"].append("Mouse")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

What to do? If the object in the dict is a string, it needs to be a list to append to it.

You can test first:

>>> if isinstance(dictio["Object"], list): 
...    dictio["Object"].append('Mouse')
... else:
...    dictio["Object"]=[dictio["Object"]]
...    dictio["Object"].append('Mouse')
... 
>>> dictio
{'Object': ['Computer', 'Mouse']}

Or try it and react to failure:

try:
    dictio["Object"].append("Mouse")
except AttributeError:
    dictio["Object"]=[dictio["Object"]]
    dictio["Object"].append("Mouse")    

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.