How not to return a None value in python

1

I have the following sample code

lista = [100,200,1000,3000,2,3,4,5,6,7,8]

def regras(x):
    if x < 100:
        if x % 2 == 0:
            return x
    if x >= 100:
        return (x+x)


v1 = list(map(regras,lista))
print(v1)

that has this output

[200, 400, 2000, 6000, 2, None, 4, None, 6, None, 8] 

How can I ignore this None ?? I even put a else with return 0 but I want it to just ignore and look like this:

[200, 400, 2000, 6000, 2, 4, 6, 8]
    
asked by anonymous 11.08.2018 / 19:19

3 answers

4

You need to learn to use else which is the counterpart of if , that is, whenever if false it falls into the else block, if there is one, in your case does not exist, the error occurs because in a given condition you return a value, and if it is false you do not return since nothing will be executed, it does not make sense, so all possible paths need to return something. And it can be used to solve the problem presented and can use another to avoid 2 if s since one is exactly the opposite of the other, in the background it is a else .

But what you really want is to filter the list (get only the results that interest you and do not do an operation on all the data, which is what map() does) and not map it. Then use the right function:

def regras(x):
    if x < 100:
        if x % 2 == 0:
            return x
    else:
        return (x + x)

lista = [100, 200, 1000, 3000, 2, 3, 4, 5, 6, 7, 8]
print(list(filter(regras,lista)))

See running on ideone . And in Coding Ground . Also I put GitHub for future reference .

The code still makes little sense because part of it does what one is for a map() , but works as expected.

Documentation for filter() .

    
11.08.2018 / 19:29
1

As Maniero commented, the logic of its function is somewhat obscure, since part of it functions as a filter, another as a map. They are conceptually distinct things that do different things.

When you use map , a new sequence will be generated with the returned function indicated for each item in the original sequence. If the function does not return value, None will be inferred, so these values appear in their final sequence.

With filter , a new sequence will be generated with the values of the original sequence for which the indicated function returns a true value which, when dealing with numbers, will be any number other than 0. Thus, if you apply a filter and return x + x , the final sequence will have the original value x , because if x is not zero, the x + x value will also be nonzero and therefore true. The filter function does not change the original values, it only filters.

So, what you need to do is separate the logics: first, filter your sequence with the desired values and then apply the map to modify them. If the intent is to remove odds less than 100, then:

filtrado = filter(lambda x: x >= 100 or x % 2 == 0, numeros)

That is, filtrado will be a string with numeros values that are greater than or equal to 100 or even. Now, just apply the map that doubles values greater than or equal to 100:

resultado = map(lambda x: x + x if x >= 100 else x, filtrado)

However, what map returns (as well as filter ) are generators, not lists. If you want as lists, you need to do the conversion:

resultado = list(map(lambda x: x + x if x >= 100 else x, filtrado))

So the code would be:

>>> numeros = [100, 200, 1000, 3000, 2, 3, 4, 5, 6, 7, 8]
>>> filtrado = filter(lambda x: x >= 100 or x % 2 == 0, numeros)
>>> resultado = list(map(lambda x: x + x if x >= 100 else x, filtrado))
>>> print(resultado)
[200, 400, 2000, 6000, 2, 4, 6, 8]
    
12.08.2018 / 15:26
0

You do not need to use two if , you can use else instead:

def regras(x):
    if x < 100:
        if x % 2 == 0:
            return x
    else:
        return (x+x)


v1 = list(map(regras,lista))
print(v1)

If you want to ignore None , just use filter to do this:

 arr  = [1, 2, 3, None, None, 4, 5]
 arr = list(filter(lambda x: x is not None, arr))

The function of filter is to create a new object containing only values that are evaluated as True by expression lambda x: x is not None , where x represents each value of its list .

See working at Ideone

I would suggest that you refactor your code to become more legible and smaller:

def regras(x):
    if x >= 100:
        return x+x    
    return x if x % 2 == 0 else None
    
11.08.2018 / 19:25