Write a function that takes list of tuples as its argument

Question

Write a function that takes a list of tuples as its only argument, where each
tuple contains three integers, and returns a list of those tuples where the sum
of the integers are 0.
For example,
tupleSum([(1,2,3),(-4,2,2),(4,5,-8),(1,0,-1),(0,0,1)])
returns:
[(-4,2,2),(1,0,-1)]

Explanation

Here in the question, we have to create a list. Where there is will be a tuple include. After that, we will call a function. So that in that list we pass the list. Also, we have to find whose tuple sum is zero. So we will add it to another list. So that at the end we will print that. Also, we write a function.

Code

#function definition to tupleSum
def tupleSum(lst):
    l = []
    #traversing lst
    for item in lst:
        s = 0
        #traversing tupple in lst
        for i in item:
            s+=i
        if s == 0:
          l.append(item) 
    #return lst
    return l
    
#creating lst
lst = [(1,2,3),(-4,2,2),(4,5,-8),(1,0,-1),(0,0,1)]
#printing lst statement
print(tupleSum(lst))
[(-4,2,2),(1,0,-1)]
[Program exited with exit code 0]

Output

takes list of tuples

 

Also read, Answer the SQL questions based on the information below

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *