可以使用以下方法判断 Python 列表是否为空:

  1. 使用 len() 函数:如果列表为空,len(list) 的值为 0,可以使用以下代码判断:
if len(list) == 0:
    print('List is empty')
  1. 直接判断列表是否为 []:如果列表为空,它的值为 [],可以使用以下代码判断:
if list == []:
    print('List is empty')
  1. 使用 not 操作符:如果列表为空,not 操作符会返回 True,可以使用以下代码判断:
if not list:
    print('List is empty')
Python 列表为空判断方法 - 3种常用技巧

原文地址: https://www.cveoy.top/t/topic/lUWE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录