How to not return a None value in Python?

Asked

Viewed 2,206 times

2

I have the following example 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 that exit

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

How can I ignore that None? I even put a else with return 0 but I want him to just ignore it and stay that way:

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

3 answers

4


You need to learn how to use the else which is the counterparty of if, that is, whenever the if fake der he falls into the block of else, if there is one, in your case there is no error, 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 makes no sense, then all possible paths need to return something. And it can be used to solve the problem presented and can use another to avoid 2 ifs since one is exactly the opposite of the other, deep down he is one else.

But what you really want is to filter the list (take only the results that matter and don’t do an operation on all the data, which is what the map() does) and does 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)))

Behold working in the ideone. And in the repl it.. Also put on the 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 of filter().

  • In fact, it is a mixed filter and map, because for values greater than or equal to 100 the value must be duplicated, so only filter will not solve the problem. You will need both filter and map.

2

As Maniero commented, the logic of its function is somewhat obscure, because part of it works as a filter, another as a map. These are conceptually different things that do different things.

When you use the map, a new sequence will be generated with the return of the given function for each item of the original sequence. If the function does not return value, it will be inferred None, so such values arise in their final sequence.

Already with the filter a new sequence will be generated with the values of the original sequence for which the given function returns a true value which, when dealing with numbers, will be any number other than 0. So, if you apply a filter and return x + x, the final sequence shall have the original value x, for if x is not zero, the value x + x will also be non-zero and therefore true. The function filter does not change the original values, just filter.

So, what you need to do is separate the logics: first, break down your sequence with the desired values and then apply the map to modify them. If the intention is to remove odd numbers smaller than 100, then:

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

That is to say, filtrado will be a sequence with the values of numeros that are greater than or equal to 100 or pairs. Now, just apply the map that doubles the 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]

0

No need for you to use two if, you can use the else in place:

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 the None, just use the 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 the values that are evaluated as True by the expression lambda x: x is not None, where x represents each value of its list.

See working on Ideone

I would suggest that you refactor your code to make it more readable and smaller:

def regras(x):
    if x >= 100:
        return x+x    
    return x if x % 2 == 0 else None
  • This does not solve the https://ideone.com/QieZFP problem

  • Saw the edition? ....

  • Yes, I did, and it’s still wrong.

  • Now yes, after I did you arranged :P But not within the example of it. but it still got bad because it runs both.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.