Computer Science, asked by shivangisudha, 1 month ago

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 anindyaadhikari13
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 Oreki
4

\textsf{\large One-Liner}

   \texttt{print('Filtered List -',}    

                \texttt{list(filter(lambda n: \hspace{-1.4em} n &lt;= 10, [2, 3, 4, 5, 20, 30, 40, 6])))}

\textsf{\large Detailed}

   \texttt{original\_list = [2, 3, 4, 5, 20, 30, 40, 6]}

   \texttt{filtered\_list = list(filter(lambda num\symbol{58} \hspace{-1.4em} num &lt;= 10, original\_list))}

   \texttt{print('Filtered List -', filtered\_list)}

\textsf{\large Algorithm}

  • \textsf{\small Using the filter method to filter out acceptable numbers from the original list.}
Attachments:
Similar questions