Answer the question
In order to leave comments, you need to log in
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))
Answer the question
In order to leave comments, you need to log in
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.
If maps and filters are optional, then:
indx_numb = [x[0] for x in enumerate(numbers) if not x[1] % 2]
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 questionAsk a Question
731 491 924 answers to any question