# python - Comment puis-je vérifier si une liste est vide

Mots clés : pythonlistpython

### meilleur 5 Réponses python - Comment puis-je vérifier si une liste est vide

92

``if not a:   print("List is empty") ``

88

``# Correct: if not seq: if seq:  # Wrong: if len(seq): if not len(seq): ``

73

``if len(li) == 0:     print('the list is empty') ``

66

``>>> x = numpy.array([0,1]) >>> if x: print("x") ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ``
``>>> x = numpy.array([0,]) >>> if x: print("x") ... else: print("No x") No x ``
``len( numpy.zeros((1,0)) ) ``
``>>> x = numpy.array([0,1]) >>> if x.size: print("x") x  >>> x = numpy.array([0,]) >>> if x.size: print("x") ... else: print("No x") x  >>> x = numpy.zeros((1,0)) >>> if x.size: print("x") ... else: print("No x") No x ``
``x = numpy.asarray(x, dtype=numpy.double) ``

53

``a = [] ``
``if not a:                           # do this!     print('a is an empty list') ``
``Yes: if not seq:      if seq:  No: if len(seq):     if not len(seq): ``
``if len(a) == 0:                     # Don't do this!     print('a is an empty list') ``
``if a == []:                         # Don't do this!     print('a is an empty list') ``
``if len(a) == 0:                     # Don't do this!     print('a is an empty list') ``
``if a == []:                     # Don't do this!     print('a is an empty list') ``
``if not a:     print('a is an empty list') ``
``>>> import timeit >>> min(timeit.repeat(lambda: len([]) == 0, repeat=100)) 0.13775854044661884 >>> min(timeit.repeat(lambda: [] == [], repeat=100)) 0.0984637276455409 >>> min(timeit.repeat(lambda: not [], repeat=100)) 0.07878462291455435 ``
``>>> min(timeit.repeat(lambda: [], repeat=100)) 0.07074015751817342 ``
``>>> import dis >>> dis.dis(lambda: len([]) == 0)   1           0 LOAD_GLOBAL              0 (len)               2 BUILD_LIST               0               4 CALL_FUNCTION            1               6 LOAD_CONST               1 (0)               8 COMPARE_OP               2 (==)              10 RETURN_VALUE ``
``>>> dis.dis(lambda: [] == [])   1           0 BUILD_LIST               0               2 BUILD_LIST               0               4 COMPARE_OP               2 (==)               6 RETURN_VALUE ``
``>>> dis.dis(lambda: not [])   1           0 BUILD_LIST               0               2 UNARY_NOT               4 RETURN_VALUE ``
``typedef struct {     PyObject_VAR_HEAD     /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */     PyObject **ob_item;      /* ob_item contains space for 'allocated' elements.  The number      * currently in use is ob_size.      * Invariants:      *     0 <= ob_size <= allocated      *     len(list) == ob_size ``
``In [1]: l = []                                                                    In [2]: %timeit l                                                                20 ns ± 0.155 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)  In [3]: %timeit not l                                                            24.4 ns ± 1.58 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)  In [4]: %timeit not not l                                                        30.1 ns ± 2.16 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) ``
``In [5]: %timeit if l: pass                                                       22.6 ns ± 0.963 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)  In [6]: %timeit if not l: pass                                                   24.4 ns ± 0.796 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)  In [7]: %timeit if not not l: pass                                               23.4 ns ± 0.793 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) ``
``In [8]: l = [1]                                                                   In [9]: %timeit if l: pass                                                       23.7 ns ± 1.06 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)  In [10]: %timeit if not l: pass                                                  23.6 ns ± 1.64 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)  In [11]: %timeit if not not l: pass                                              26.3 ns ± 1 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) ``