How to check if a tuple is empty in Python?

Just like lists, tuples are also a sequence of values and are considered falsy if they are empty. So, if you want to check if a tuple is empty, you can simply use the not operator.

empty_tuple = ()
non_empty_tuple = (1, 2, 3)

not empty_tuple # True…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Isabelle M.

Just like lists, tuples are also a sequence of values and are considered falsy if they are empty. So, if you want to check if a tuple is empty, you can simply use the not operator.

empty_tuple = ()
non_empty_tuple = (1, 2, 3)

not empty_tuple # True
not non_empty_tuple # False

In the above code example, we can see that the not operator returns True if the tuple is empty and False if the tuple is not empty. The not operator is a unary operator that returns the opposite of the value it is applied to. Using the not operator is considered the most Pythonic way to check if a object is empty.

There are other ways to check if a tuple is empty. For example, you can use the len() function. This function returns the length of the tuple, which is 0 if the tuple is empty.

empty_tuple = ()
non_empty_tuple = (1, 2, 3)

len(empty_tuple) == 0 # True
len(non_empty_tuple) == 0 # False

One more way to check if a tuple is empty is to compare the tuple to an empty tuple using the == operator.

empty_tuple = ()
non_empty_tuple = (1, 2, 3)

empty_tuple == () # True
non_empty_tuple == () # False


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Isabelle M.


Print Share Comment Cite Upload Translate Updates
APA

Isabelle M. | Sciencx (2022-12-24T22:00:00+00:00) How to check if a tuple is empty in Python?. Retrieved from https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/

MLA
" » How to check if a tuple is empty in Python?." Isabelle M. | Sciencx - Saturday December 24, 2022, https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/
HARVARD
Isabelle M. | Sciencx Saturday December 24, 2022 » How to check if a tuple is empty in Python?., viewed ,<https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/>
VANCOUVER
Isabelle M. | Sciencx - » How to check if a tuple is empty in Python?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/
CHICAGO
" » How to check if a tuple is empty in Python?." Isabelle M. | Sciencx - Accessed . https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/
IEEE
" » How to check if a tuple is empty in Python?." Isabelle M. | Sciencx [Online]. Available: https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/. [Accessed: ]
rf:citation
» How to check if a tuple is empty in Python? | Isabelle M. | Sciencx | https://www.scien.cx/2022/12/24/how-to-check-if-a-tuple-is-empty-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.