How do I know if a list is empty in Python?

3

I'm starting in python and always end up with some doubts in the beginning.

I need to know how to test if a list is empty.

As I always say, I come from PHP.

So I used to do these forms:

count($array) == 0

empty($array);

!$array

How to do this in python ?

Example:

 a = [1, 2, 3]
 b = []

 # a é vazio?
 # b é vazio?
    
asked by anonymous 15.10.2015 / 21:23

2 answers

6

I suppose a a list:

a = []

Test yourself like this:

if not a:
    # Condição se for vazio.

Yes. Very simple.

    
15.10.2015 / 21:23
4

Just for curiosity purposes. You can also do the "ugly way".

Still assuming that a is a list

a = []

if len(a) == 0:
    # Condição se for vazio

if a == []:
    # Condição ser for vazio
    
15.10.2015 / 21:48