Given L=[10,20,30,40,50,60,70,80,90,100], how will you get a list [20, 40, 60, 80]
Answers
Python programming:
L=[10,20,30,40,50,60,70,80,90,100]
print(L[1:8:2])
Solution:
Assuming that the language is Python, we can solve this question in 2 ways.
Approach 1.
L=[10,20,30,40,50,60,70,80,90,100]
print(L[1::2])
> [20, 40, 60, 80, 100]
Here, the required numbers are present in indexes 1, 3, 5, 7, 9. So, start value = 1, end value = 9 (optional) and step value is 2.
Using list slicing, we have extracted the numbers by writing - L[1 : : 2]
Approach 2.
L=[10,20,30,40,50,60,70,80,90,100]
print(list(filter(lambda x:x%20==0,L)))
> [20, 40, 60, 80, 100]
Here, we can observe that the numbers which are multiple of 20 are taken out. So, using filter() method, we have filtered out by giving a condition that - the number must be divisible by 20.
See the attachment for verification.