write a program to generate a list containing elements less than 10 from list l1=[2,3,4,5,20,30,40,6]
Answers
Answered by
1
Required Answer:-
Question:
- Write a program to generate a list containing elements less than 10 from the list l1=[2,3,4,5,20,30,40,6]
Solution:
Here comes the program. It is done using loop.
l1=[2,3,4,5,20,30,40,6]
l2=[]
for i in l1:
if i<10:
l2.append(i)
print("List generated successfully. Here is the list.")
print(l2)
Logic: Iterate a loop and check if the number is less than 10. If true, add the number to the new list.
There is another shorter approach to this question. Here is it,
l1=[2,3,4,5,20,30,40,6]
l2=[i for i in l1 if i<10]
print("List generated successfully. Here is the list.")
print(l2)
See the attachment for output ☑.
Attachments:
Answered by
4
Attachments:
Similar questions