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.

Answered by Waseem A. Python tutor

1561 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a function that takes an imput (call it usr_in), the user's age (usr_age) and the user's name (usr_nm) and outputs the following string: "Hi, My name is [usr_nm], I am [usr_age] years old, and my favorite number is [usr_in]"


Explain how you would write a python program that takes a rectangle and a point in a 2D space as command-line arguments and checks if they intersect.


How do you write code to implement a recursive fibonacci algorithm?


Give an example of a FOR loop


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy