D
D
demchenkodenis2021-07-01 09:14:46
Python
demchenkodenis, 2021-07-01 09:14:46

How to use the map() filter() functions to store the indexes of the even elements of another array in an array?

Good afternoon!
Please tell me to solve the issue: Save the indexes of the even elements of another array in the array.
Used code

numbers = [1, 2, 3, 4, 5, 6]
indx_numb = list(filter(lambda x: numbers[x] % 2 == 0, numbers))

gave an error list index out of range. The same is true when using map()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan., 2021-07-01
@LaRN

The element itself is passed to the lambda, not its index, hence the error.
In theory, you need something like this
lambda x: x% 2 == 0
But with this option, the elements themselves will be returned and not their indices.

D
Dmitry, 2021-07-01
@CrazzyDi

If maps and filters are optional, then:

indx_numb = [x[0] for x in enumerate(numbers) if not x[1] % 2]

D
demchenkodenis, 2021-07-01
@demchenkodenis

Guys thanks for the help! I deal with the topic of code efficiency through the use of list comprehensions, map () using the example of one task with enumerate () you can also do this

indx_numb = v for k, v in enumerate(nums) if not k % 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question