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]
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 bothfilter
andmap
.– Woss