0

I have a dictionary like so:

d = {}
d['key1'] = [('tuple1a', 'tuple1b', ['af1', 'af2', 'af3']),
            ('tuple2a', 'tuple2b', ['af4', 'af5', 'af6']),
            ('tuple3a', 'tuple3b', ['af7', 'af8', 'af9'])]      

I want to write a function to allow me to update the list portion of the value (e.g. ['af1','af2','af3']). The code below works to filter by the different values to get to the correct list within the value:

def update_dict(dictionary, key, tuple_a, tuple_b, new_list=None):

    for k,v in dictionary.items():
        if key in k:
            for i in v:
                if tuple_a in i:
                    if tuple_b in i:
                        #di.update(i[2], new_lst) #this is what I'd like to do but can't get the right syntax
    return dictionary

I want to add something like di.update(i[2], new_lst) My question is how can I update ONLY the list value with a new list?

e9e9s
  • 885
  • 2
  • 13
  • 24
  • It is a dictionary of tuples. You cannot update the tuples. But I wonder f you can change the referring list separately. – AkshayDandekar May 11 '17 at 15:43
  • Since tuples are immutable I can re-create the dictionary like this: `d.update({k: [(tuple_a, tuple_b, aod_nt)]})` but it creates a dictionary of ONLY that one key:value pair. How can I preserve the other values in the dictionary? – e9e9s May 11 '17 at 16:32
  • I [posted an answer](https://stackoverflow.com/a/60514950/1904943) elsewhere on Stackoverflow that describes how to change a value within a list within a dictionary. – Victoria Stuart Mar 03 '20 at 20:27

2 Answers2

1

Since tuple is an immutable type, you cannot change a single entry in the tuple. A workaround is to create a list with the elements you would like to have in the tuple, and then create a tuple from the list. You will also have to assign the new tuple to the given element in the parent-list, like this:

for k,v in dictionary.items():
    if key in k:
        for n,tpl in enumerate(v):
            if tuple_a in tpl and tuple_b in tpl:
                v[n] = tuple( list(tpl)[:-1] + [new_list] )

(I was a little confused by your example, in which the variables called tuple_a and tuple_b were actually strings. It might have been better to call them name_a and name_b or similar.)

sveinbr
  • 106
  • 5
1

As other mentioned you can not change a single entry in the tuple. But the list within the tuple is still mutable.

>>> my_tuple = ('a', 'b', 'c', [1, 2, 3, 4, 5], 'd')
>>> my_tuple
('a', 'b', 'c', [1, 2, 3, 4, 5], 'd')
>>> my_tuple[3].pop()
5
>>> my_tuple[3].append(6)
>>> my_tuple
('a', 'b', 'c', [1, 2, 3, 4, 6], 'd')

So for what you want, you can do something like:

>>> my_tuple = ('a', 'b', 'c', [1, 2, 3, 4, 5], 'd')
>>> newList = [10, 20, 30]
>>>
>>> del my_tuple[3][:]       # Empties the list within
>>> my_tuple
('a', 'b', 'c', [], 'd')
>>> my_tuple[3].extend(newList)
>>> my_tuple
('a', 'b', 'c', [10, 20, 30], 'd')

So in your code replace the # di.update(i[2], new_lst) with

del i[2][:]
i[2].extend(new_list)

And I think this is faster too.

yeniv
  • 1,589
  • 11
  • 25