Python入門 リストが空かどうかを確認するにはどうすればよいですか?

リストが空かどうかを確認するにはどうすればよいですか?

方法 1: Python で「not」演算子を使用して入力リストが空かどうかを確認する方法

#!/usr/bin/env python3

def How_to_Check_If_a_List_Is_Empty_in_Python_1():
myList = []
  if not myList:
    print("The list is empty")
  else:
    print("The list is not empty")

How_to_Check_If_a_List_Is_Empty_in_Python_1()

方法 2: Python の「bool()」関数を使用して入力リストが空かどうかを確認する方法

#!/usr/bin/env python3

def How_to_Check_If_a_List_Is_Empty_in_Python_2():
myList2 = [ ]
  if not bool(myList2):
    print("The list is empty")
  else:
    print("The list is not empty")

How_to_Check_If_a_List_Is_Empty_in_Python_2()

方法 3: Python の「len()」関数を使用して入力リストが空かどうかを確認する方法

#!/usr/bin/env python3

def How_to_Check_If_a_List_Is_Empty_in_Python_3():
  myList2 = [ ]
  if len(myList2) == 0:
    print("The list is empty")
  else:
    print("The list is not empty")

How_to_Check_If_a_List_Is_Empty_in_Python_3()

リストが空かどうかを確認するために、Python では複数の関数/演算子を使用できます。

1. ifに組み込まれるnot演算子、
2. オブジェクトのブール値を返すbool()関数、
3. true false などオブジェクト内の項目の数を返すlen()関数。

3つの方法でリストが空かどうかを確認する事ができます。

書籍の紹介

Python入門 文字列を追加するにはどうすればよいですか?

Python入門 文字列を追加するにはどうすればよいですか?

Nクイーン問題(37)第六章 C言語移植 その17 pthread並列処理完成

Nクイーン問題(37)第六章 C言語移植 その17 pthread並列処理完成