KnowledgeBoat Logo

Computer Science

Which of the following is the correct expansion of list1 = [expr(i) for i in list0 if func(i)] ?

(a)

list_1 = []
for i in list_0:
    if func(i):
        list_1.append(i)

(b)

for i in list_0:
    if func(i):
        list_1.append(expr(i))

(c)

list_1 = []
for i in list_0:
    if func(i):
        list_1.append(expr(i))

(d) none of these

Linear Lists

3 Likes

Answer

list_1 = []
for i in list_0:
    if func(i):
        list_1.append(expr(i))

Reason —

  1. for i in list_0 — This part iterates over each element i in the list list_0.
  2. if func(i) — This part applies the function func() to each element i and checks if the result is True. Only elements for which func(i) returns True will be included in the resulting list.
  3. expr(i) — This part applies some expression or function call expr() to each selected element i.

Therefore, the correct expansion would be:

list_1 = []
for i in list_0:
    if func(i):
        list_1.append(expr(i))

Answered By

3 Likes


Related Questions