How to check if there's a tuple inside a list?

  • A+


seq = [(1, 2), (3, 4), (5, 6)] print(() in seq) 

Returns False? How can I check if there's a tuple, or even a generic sequence, inside a sequence? (With no specific values, as in this answer)


() is an empty tuple. seq does not contain an empty tuple.

You want

>>> seq = [(1, 2), (3, 4), (5, 6)] >>> any(isinstance(x, tuple) for x in seq) True 

For a generic sequence you can use

>>> from collections import abc >>> any(isinstance(x, abc.Sequence) for x in seq) True 

However, lots of objects are informally treated as sequences but neither implement the full protocol abc.Sequence defines nor register as a virtual subclass of Sequence.

Read this excellent answer for additional information.

You can find a question about detecting sequences here.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: