15,617,020 members
1.00/5 (1 vote)
See more:
Define and implement a function that accepts a tuple of lists and returns the sum of only the unique numbers in that tuple.

For example, if the user enters the following tuple: ([1, 5], [7, 8, 3, 2], [3, 3, 2]) then the output should be 26

What I have tried:

I have tried a couple things but nothing seems to be working. I know this is a very basic question but I am very new to Python. I'd appreciate your answer. Many thanks
Posted
Updated 13-Mar-22 21:51pm
Maciej Los 13-Mar-22 17:22pm
How 1+5+7+8 can be equal to 26?

## Solution 1

I'd do that this way:
1) create dictionary object and count the number of occurencies of specific number in the entire tuple,
2) create list of numbers which occurence is equal to 1
3) get sum of numbers (step 2)

It's pretty easy.

v2
BeginnerCoder1 13-Mar-22 20:45pm
Hey thankyou so much for your answer. Actually I'm looking for the exact coding of this, would you be kind enough to write the code please?
Dave Kreskowiak 13-Mar-22 21:24pm

If you don't write the code, you learn nothing.
BeginnerCoder1 13-Mar-22 21:26pm
Well said Dave. But if you don't want to answer just ignore the message why bother leaving a comment like this.
CPallini 14-Mar-22 3:45am
5.
Maciej Los 14-Mar-22 5:38am
Thank you, Carlo.

## Solution 2

You ca do it with a one-liner. Try
Python
```tin = ([1, 5], [7, 8, 3, 2], [3, 3, 2]) # the input data
print(sum ({x for l in tin for x in l})) # the one-liner solution```
Explanation: a set comprehension is used to extract all the unique numbers of `tin` into a set (because sets don't store duplicate values). Eventually all the set items are summed up using the `sum` function.

Richard MacCutchan 14-Mar-22 4:24am
+5; very cool.
CPallini 14-Mar-22 4:29am
Thank you!
Maciej Los 14-Mar-22 5:39am
5ed!
CPallini 15-Mar-22 2:56am
Thank you!
BeginnerCoder1 14-Mar-22 18:55pm
@CPallini THANK YOU