ignore in function call if empty

2

suppose I'm feeding a function with a tuple:

a = ['c:/', 'd:/', 'x:/', 'y:/']
b = ['c:/data', 'd:/data']
funcao((a, b))

however if you have a list that is empty you would like to ignore example:

funcao((se_vazio_ignore(a), se_vazio_ignore(b)))

Is it possible to do this?

    
asked by anonymous 25.07.2016 / 20:49

2 answers

1

If I understand, something like this already solves:

if len(a) > 0 and len(b) > 0:
    funcao((a, b))

EDIT

In fact you also need to check if any of the elements are not null:

if len(a) > 0 and len(b) > 0 and all(e != '' for e in a) and all(e != '' for e in b):
    funcao((a, b))
    
25.07.2016 / 21:03
1

If a listener is empty, and the function is to use list elements, you do not need to do and - the body of for that will use the list will simply not run.

def funcao(x):
   for lista in x:
       for elemento in lista:
           # fazer coisas

funcao((a, b))

Ready, in the event that "a" is an empty list the internal% wc will simply not be executed.

In other situations you may want to go through some other value in case your variable is an empty list, or None, or another false value. In this case, you can use the short circuit of the for operator - in the same way that it is used in lignations with the syntax derived from C (which use or as the "or" operator).

In this case, they could not be used directly in || : it would give an error saying that None is not interoperable:

/ p>

funcao((a or [], b or []))

(to call the same function as above, ensuring that each element is iterable).

    
26.07.2016 / 19:29