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 —
for i in list_0
— This part iterates over each elementi
in the listlist_0
.if func(i)
— This part applies the functionfunc()
to each elementi
and checks if the result is True. Only elements for whichfunc(i)
returns True will be included in the resulting list.expr(i)
— This part applies some expression or function callexpr()
to each selected elementi
.
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
What will be the output of the following Python code ?
lst = [3, 4, 6, 1, 2] lst[1:2] = [7,8] print(lst)
- [3, 7, 8, 6, 1, 2]
- Syntax error
- [3, [7, 8], 6, 1, 2]
- [3, 4, 6, 7, 8]
What will be the output of the following Python code snippet ?
k = [print(i) for i in my_string if i not in "aeiou"]
- prints all the vowels in my_string
- prints all the consonants in my_string
- prints all characters of my_string that aren't vowels
- prints only on executing print(k)
Fill in the blanks:
A _________ is a named group of data of different data types which can be processed as a single unit.
Fill in the blanks:
In _________, each element of the list is compared with the given item to be reached for, one-by-one.