How to test if all elements are 2-element tuples

Asked

Viewed 55 times

0

I am solving an exercise that I first have to test whether the argument received is a Double. If yes, I have to test if all its elements are tuples of 2 elements, where the first is a string, and the second is an integer.

I know that to test whether it is Thiuplo is used:

isinstance(t[i], tuple)

To test if 1st element is string:

isinstance(t[0], str)

To test if the 2nd element is integer:

isinstance(t[1], int)

My question is: how do I test whether all the elements of Tuplo are 2-element tuples?

1 answer

0

def testar(argumento):
    return (
        isinstance(argumento, tuple)
        and all(isinstance(elemento, tuple) 
                and len(elemento) == 2
                and isinstance(elemento[0], str)
                and isinstance(elemento[1], int)
            for elemento in argumento)
    )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.