5

the current code I have is category1[name]=(number) however if the same name comes up the value in the dictionary is replaced by the new number how would I make it so instead of the value being replaced the original value is kept and the new value is also added, giving the key two values now, thanks.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
jsnabs2
  • 113
  • 2
  • 2
  • 5
  • 3
    a key only has one value, you would need to make the value a tuple or list etc – Calum Mar 25 '15 at 03:01
  • @Calum is correct, and should probably reproduce his/her comment as an answer. You'll need to change your representation of your values to accommodate your desired semantics. – BlackVegetable Mar 25 '15 at 03:03
  • hint: `collections.defaultdict(list)` – wim Mar 25 '15 at 03:04

6 Answers6

4

You would have to make the dictionary point to lists instead of numbers, for example if you had two numbers for category cat1:

categories["cat1"] = [21, 78]

To make sure you add the new numbers to the list rather than replacing them, check it's in there first before adding it:

cat_val = # Some value
if cat_key in categories:
    categories[cat_key].append(cat_val)
else:
    # Initialise it to a list containing one item
    categories[cat_key] = [cat_val]

To access the values, you simply use categories[cat_key] which would return [12] if there was one key with the value 12, and [12, 95] if there were two values for that key.

Note that if you don't want to store duplicate keys you can use a set rather than a list:

cat_val = # Some value
if cat_key in categories:
    categories[cat_key].add(cat_val)
else:
    # Initialise it to a set containing one item
    categories[cat_key] = set(cat_val)
Will Richardson
  • 7,780
  • 7
  • 42
  • 56
0

You can create a dictionary in which you map a key to a list of values, in which you would want to append a new value to the lists of values stored at each key.

           d = dict([])
           d["name"] = 1
           x = d["name"]
           d["name"] = [1] + x
Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44
0

a key only has one value, you would need to make the value a tuple or list etc

If you know you are going to have multiple values for a key then i suggest you make the values capable of handling this when they are created

Calum
  • 2,110
  • 2
  • 22
  • 39
0

It's a little hard to understand your question.

I think you want this:

>>> d[key] = [4]
>>> d[key].append(5)
>>> d[key]
[4, 5]
Chris Drake
  • 353
  • 1
  • 7
0

Depending on what you expect, you could check if name - a key in your dictionary - already exists. If so, you might be able to change its current value to a list, containing both the previous and the new value.

I didn't test this, but maybe you want something like this:

mydict = {'key_1' : 'value_1', 'key_2' : 'value_2'}

another_key = 'key_2'
another_value = 'value_3'

if another_key in mydict.keys():
    # another_key does already exist in mydict
    mydict[another_key] = [mydict[another_key], another_value]

else:
    # another_key doesn't exist in mydict
    mydict[another_key] = another_value

Be careful when doing this more than one time! If it could happen that you want to store more than two values, you might want to add another check - to see if mydict[another_key] already is a list. If so, use .append() to add the third, fourth, ... value to it.

Otherwise you would get a collection of nested lists.

xph
  • 937
  • 3
  • 8
  • 16
0

I guess this is the easiest way:

category1 = {}
category1['firstKey'] = [7]
category1['firstKey'] += [9]
category1['firstKey']

should give you:

[7, 9]

So, just use lists of numbers instead of numbers.

TheRevanchist
  • 331
  • 1
  • 4
  • 12