my_list = [i for i in range(5, 0, -1)] # Creates a list: [5, 4, 3, 2, 1]
m = [my_list[i] for i in range(len(my_list)) if my_list[i] % 2 == 0] # Include only even numbers
print(m) #[4, 2]
my_list = [i for i in range(5,0,-1)]
m=[my_list[i] for i in range(5)] if my_list[i]%2==0
print(m)
#output SyntaxError: expected 'else' after 'if' expression
Ans is D
Answer is indeed D, due to the ] before the if statement.
If this bracket wouldnt be there, the result would be [4, 2].
my_list = [i for i in range(5,0,-1)]
m = [my_list[i] for i in range(5) if my_list[i] % 2 == 0]
print(m)
[4, 2]
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
mldprasad
1 month, 2 weeks agoseaverick
12 months agokontra
1 year, 8 months agogekkehenk
1 year, 9 months agoandr3
1 year, 10 months agoRizos
1 year, 10 months agobesha
2 years, 6 months agoangelika_az
2 years, 7 months agomacxsz
2 years, 8 months ago