Check list empty C#

Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

my_list = [] if not my_list: print["the list is empty"]

Output

the list is empty

If 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 empty

In 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

The list is empty

[] 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

Returns whether the list container is empty [i.e. whether its size is 0].

This function does not modify the container in any way. To clear the content of a list container, see list::clear.



none

true if the container size is 0, false otherwise.

123456789101112131415161718192021 // list::empty #include #include int main [] { std::list mylist; int sum [0]; for [int i=1;i

Chủ Đề