How to test if all elements are tuples of 2 elements [duplicate]

0

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

I know that to test if it is touble 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 if all the elements of the tuple are tuples of 2 elements?

    
asked by anonymous 09.11.2018 / 03:00

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)
    )
    
09.11.2018 / 06:07