100

I want to add multiple values to a specific key in a python dictionary. How can I do that?

a = {}
a["abc"] = 1
a["abc"] = 2

This will replace the value of a["abc"] from 1 to 2.

What I want instead is for a["abc"] to have multiple values(both 1 and 2):

  1. how to create that from start that it look like this:
    a["abs"] -> [1, 2]
  2. how to add to that key more values...
nirportal
  • 3
  • 4
PythonEnthusiast
  • 15,691
  • 41
  • 122
  • 238

2 Answers2

173

Make the value a list, e.g.

a["abc"] = [1, 2, "bob"]

UPDATE:

There are a couple of ways to add values to key, and to create a list if one isn't already there. I'll show one such method in little steps.

key = "somekey"
a.setdefault(key, [])
a[key].append(1)

Results:

>>> a
{'somekey': [1]}

Next, try:

key = "somekey"
a.setdefault(key, [])
a[key].append(2)

Results:

>>> a
{'somekey': [1, 2]}

The magic of setdefault is that it initializes the value for that key if that key is not defined, otherwise it does nothing. Now, noting that setdefault returns the key you can combine these into a single line:

a.setdefault("somekey",[]).append("bob")

Results:

>>> a
{'somekey': [1, 2, 'bob']}

You should look at the dict methods, in particular the get() method, and do some experiments to get comfortable with this.

President James K. Polk
  • 38,341
  • 16
  • 90
  • 119
  • 14
    take a look at the posted times - we both gave the same answer at exactly the same time, down to the second :) – MattDMo Dec 14 '13 at 17:11
  • I want to add one by one. – PythonEnthusiast Dec 14 '13 at 17:12
  • like a["abc"] = 1 and the a["abc"]="def" – PythonEnthusiast Dec 14 '13 at 17:12
  • 1
    Like I provide a key and value to a function. If key is already there, then it should append the new value to the key. – PythonEnthusiast Dec 14 '13 at 17:15
  • What if i dont want to use the list. Can I still append the value? For eg : I've a dict a["abc":1]. Now I want to add "def" to "abc" key. How can I do that? – PythonEnthusiast Dec 14 '13 at 17:20
  • 1
    You can check whether type(a["abc"]) == list. If not, then create a new list with that item as the first element in that list. – octref Dec 14 '13 at 17:24
  • And my suggestion is to make the value a list. If there is only one element, then make a list with that sole element. – octref Dec 14 '13 at 17:26
  • @user1162512. If you added 1 and "def" to "abc" key, then did `x = a["abc"]`, what would you expect `x` to contain? – ekhumoro Dec 14 '13 at 18:10
  • How do you work around with duplicates if you append lists? Say dictionary[names] = ["Bob", "Diana", "John"] then I append a["names"].append(["Bob", "Peter"]). My dictionary is now: {'names': ['Bob', 'Diana', 'John', ['Bob', 'Peter']]} How can I check it for duplicates and remove second Bob? – jester112358 Jan 21 '15 at 14:26
  • 2
    Thanks, really liked a.setdefault("somekey",[]).append("bob") – ARH Feb 28 '15 at 18:26
  • @James Can I add multiple lists as value ? – diffracteD Jul 19 '16 at 05:28
25

How about

a["abc"] = [1, 2]

This will result in:

>>> a
{'abc': [1, 2]}

Is that what you were looking for?

MattDMo
  • 96,286
  • 20
  • 232
  • 224