Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .

A simple solution to this problem would be as follows [x for x in list[::2] if x%2 == 0] For example, given the following list: list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ] The list comprehension [x for x in list[::2] if x%2 == 0] will evaluate to: [10, 18, 78] The expression works by first taking the numbers that are at the even indices, and then filtering out all the odd numbers.

WA

Related Python Mentoring answers

All answers ▸

Write a function that takes a list of numbers as input, and outputs the average of the numbers. The function should catch any errors.


What is the outcome of the following code? print(2*[3,4,5])


Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.


Give a segment of python code that will print the numbers 1 to 7, each one on a new line