inspiring
inspiring

Reputation: 123

How do I recursively add a new tuple if it is not in the nested tuple already?

Currently, I have a nested tuple:

t = (('b', 2), ('a', 1), ('c', 3))

I am looking to pass in two arguments key and value and if they are not in the nested tuple, add it to the tuple like this (key, value), and return the new tuple.

If the key is in the tuple, it would simply replace it.


For Example:

Using the nested tuple t I would like to pass in key = d and value = 4. Because, the tuple is not in the nested tuple, it would be added into the nested tuple.

The expected output would be: (('b', 2), ('a', 1), ('c', 3), ('d', 4))

If the tuple is already in the nested tuple, it would be replaced. For example, if I pass in key = c and value = 9 to the nested tuple t

The expected output would be: (('b', 2), ('a', 1), ('c', 9))


What I have so far...

def add_new(t, key, value)
    if len(t) == 0:
        return (key, value)
    if key not in t[0][0] and value not in t[0][1]:
       return (key, value,) + add_new(t[0:], key, value)
    if key in t[0][0] and value in t[0][1]:
       return (key, value,) + add_new(t[1:], key, value)

add_new(t, "d", 4)
add_new(t, "c", 9)

Upvotes: 1

Views: 752

Answers (3)

Nikolas Stevenson-Molnar
Nikolas Stevenson-Molnar

Reputation: 4710

You can do this with a generator expression to create a new tuple with only the values you want, then add the new tuple:

def add_new(t, key, value):
    return tuple(item for item in t if item[0] != key) + ((key, value),)

>> t = (('b', 2), ('a', 1), ('c', 3), ('d', 4))
>> add_new(t, 'c', 9)
(('b', 2), ('a', 1), ('d', 4), ('c', 9))

Note the extra comma in ((key, value),). Otherwise, it'll be a single tuple instead of a nested one.

Upvotes: 1

slider
slider

Reputation: 12990

If you absolutely have to use recursion, you can choose your base case as an empty tuple or a tuple where the first element's key matches key. In this case, you append an updated key to the final result. Otherwise, you return by keeping the first element as is and recursively calling add_new on the remaining elements.

This results in the following function:

def add_new(t, key, value):
    if len(t) == 0 or t[0][0] == key:
        return ((key, value),) + t[1:]
    return t[0:1] + add_new(t[1:], key, value) 

tup = (('b', 2), ('a', 1), ('c', 3))
print(add_new(tup, "d", 4))
print(add_new(tup, "c", 9))

Output

(('b', 2), ('a', 1), ('c', 3), ('d', 4))
(('b', 2), ('a', 1), ('c', 9))

Upvotes: 1

Clemson
Clemson

Reputation: 506

I think your best bet would be to use a dict, rather than a tuple.

t = {
      'b': 2,
      'a': 1,
      'c': 3,
    }

Then, when you want to add another variable, you can just use

t['d'] = 4

Additionally, when you want to replace a value which already exists, then you use

t['c'] = 9

There are several advantages to this. Dictionaries are in generally much faster than indexed data structures like Tuples or Lists for adding, replacing and removing values. They are also very intuitive and do not require that you define any of your own functions or methods to use them. See here for more information on python dictionaries.

Upvotes: 0

Related Questions