How do you add multiple tuples(lists, whatever) to a single dictionary key without merging them?

46,304

Solution 1

Just map your key to a list, and append tuples to the list.

d = {'Key1': [(1.000,2.003,3.0029)]}

Then later..

d['Key1'].append((2.3232,13.5232,1325.123))

Now you have:

{'Key1': [(1.0, 2.003, 3.0029), (2.3232, 13.5232, 1325.123)]}

Solution 2

Use defaultdict and always use append and this will be seemless.

from collections import defaultdict

x = defaultdict(list)
x['Key1'].append((1.000,2.003,3.0029))

Solution 3

A dictionary value can't contain two tuples just by themselves. Each dictionary key maps to a single value, so the only way you can have two separate tuples associated with that key is for them to be themselves contained within a tuple or list: {'Key1':[(1.000,2.003,3.0029),(2.3232,13.5232,1325.123)]} - note the extra pair of square brackets.

One way of doing this would be to get the current value associated with your key, and append it to a list before setting the new list back to that key. But if there's a possibility you'll need that for any key, you should do it for all keys right at the start, otherwise you'll get into all sorts of difficulties working out what level you're at.

Solution 4

Instead of:

{'Key1':(1.000,2.003,3.0029)}

What you want is:

{'Key1':[(1.000,2.003,3.0029)]}

When you add another tuple, you'll get:

{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]}

Solution 5

I think your questions is somewhat badly formulated. You want to:

  1. Associate a tuple to a key, if the key is not in the dictionary
  2. Replace the tuple with a list of two tuples, if the key is in the dictionary and points to a tuple
  3. Append a tuple to the list of tuples associated to a key

This could be achieved with the following code:

def insertTuple(d, k, tup):
    if k not in d:
        d[k] = tup
    elif type(d[k]) == tuple:
        d[k] = [ d[k], tup ]
    else:
        d[k].append(tup)
Share:
46,304
Jason White
Author by

Jason White

Updated on February 22, 2020

Comments

  • Jason White
    Jason White about 4 years

    I've been trying to figure out how to add multiple tuples that contain multiple values to to a single key in a dictionary. But with no success so far. I can add the values to a tuple or list, but I can't figure out how to add a tuple so that the key will now have 2 tuples containing values, as opposed to one tuple with all of them.

    For instance say the dictionary = {'Key1':(1.000,2.003,3.0029)}

    and I want to add (2.3232,13.5232,1325.123) so that I end up with:

    dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))} (forgot a set of brackets!)

    If someone knows how this can be done I'd appreciate the help as it's really starting to annoy me now.

    Thanks!

    Edit: Thanks everyone! Ironic that I tried that except at the time I was trying to make the value multiple lists instead of multiple tuples; when the solution was to just enclose the tuples in a list. Ah the irony.

  • glglgl
    glglgl about 12 years
    This approach will both add incertainty to this insert...() method as well as to the evaluation code.
  • user1202136
    user1202136 about 12 years
    @glglgl I agree, it is ugly. But it seems to be what the OP wanted.