Why is the output coming out like that?

Asked

Viewed 110 times

5

I’m trying to make a program in which I use a list and this program returns 2 lists with pairs other with the odd.

lista = [0,1,2,3,4,5,6,7,8,9,10]
def par(numero):
    if numero % 2 == 0:
        return numero
def impar(numero):
    if numero % 2 != 0:
        return numero

par = list(map(par,lista))
impar = list(map(impar,lista))
print(par)
print(impar)

where the output he gives me is: [0, None, 2, None, 4, None, 6, None, 8, None, 10] [None, 1, None, 3, None, 5, None, 7, None, 9, None] Is there any way to make the program without these Nones occurring? PS have to do it with the higher order functions hence the use of the map

2 answers

11

The reason of None is because the function map performs its functions for each element in the list, so that a return on these functions is always necessary. As you do not return anything in the else (that does not exist in its functions), Python assumes None in such cases.

If you are required to use the map, a possible solution is to simply remove the None after the call from map, maybe using a understanding:

lista = [0,1,2,3,4,5,6,7,8,9,10]
def par(numero):
    if numero % 2 == 0:
        return numero
def impar(numero):
    if numero % 2 != 0:
        return numero

par = [i for i in list(map(par,lista)) if i is not None]
impar = [i for i in list(map(impar,lista)) if i is not None]

print(par)
print(impar)

Note that while doing par = ... you change your function par previously defined so that it can no longer be used. Maybe it would be good to use another variable name. :)

If on the other hand you don’t need to use the map, does directly:

lista = [0,1,2,3,4,5,6,7,8,9,10]

par = [i for i in lista if i % 2 == 0]
impar = [i for i in lista if i % 2 != 0]

print(par)
print(impar)

There is still another option. If you are using Numpy, you can do so:

import numpy as np
lista = np.array([0,1,2,3,4,5,6,7,8,9,10])

paridx = np.logical_not((lista % 2).astype(bool))
imparidx = (lista % 2).astype(bool)

par = lista[paridx]
impar = lista[imparidx]

print(par)
print(impar)

Explaining:

  1. The command lista % 2 returns a list with the leftover division of each element by 2, which for your list it will return [0 1 0 1 0 1 0 1 0 1 0].
  2. The command (lista % 2).astype(bool) simply converts these 0s and 1s to logical values, making this list become [False True False True False True False True False True False]. Note how basically it indicates False where the rest of the split by 2 is zero, and True where the rest of the division by 2 is 1. That is, basically it indicates true where the number at that position (index) is odd.
  3. That’s why the final command is (lista % 2).astype(bool) to indicate the odd number indices and np.logical_not((lista % 2).astype(bool)) (the logical negation of these values) to indicate the indices of even numbers.
  4. Finally, these indexes are used directly to "filter" in the original list the items where the index indicates true (True) - a very nice and useful resource when manipulating data: par = lista[paridx] and impar = lista[imparidx].

2

The answer from @Luiz Vieira, is perfect. Together only one more variant: use of filter (pass only the values approved by a filter function).

Example:

 lista=range(11)
 par = filter(lambda x: x % 2 == 0 , lista)

Browser other questions tagged

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