Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Have an account? Sign Inmy_list = [] if not my_list: print["the list is empty"]
Output
the list is emptyIf my_list is empty then not returns True.
It is the most pythonic way of testing emptiness. If you want to learn more about boolean truth value, you can refer to Truth Value Testing.
Example 2: Using len[]
my_list = [] if not len[my_list]: print["the list is empty"]Output
the list is emptyIn this example, length of list is used to check if there is any element in the list. If the length of a list is 0, then the list is empty.
Example 3: Comparing with []
my_list = [] if my_list == []: print["The list is empty"]Output
[] is an empty list, therefore if my_list has no elements, then it should be equal to [].
public member function
bool empty[] const noexcept;
Test whether container is empty
This function does not modify the container in any way. To clear the content of a list container, see list::clear.
123456789101112131415161718192021 | // list::empty
#include
#include int main []
{ std::list mylist; int sum [0]; for [int i=1;i
Chủ Đề |