Python Multiple Choice Questions – List Comprehension

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “List Comprehension”.

1. What will be the output of the following Python code snippet?

k = [print(i) for i in my_string if i not in "aeiou"]

a) prints all the vowels in my_string
b) prints all the consonants in my_string
c) prints all characters of my_string that aren’t vowels
d) prints only on executing print(k)
View Answer

Answer: c
Explanation: print(i) is executed if the given character is not a vowel.
advertisement
advertisement

2. What is the output of print(k) in the following Python code snippet?

k = [print(i) for i in my_string if i not in "aeiou"]
print(k)

a) all characters of my_string that aren’t vowels
b) a list of Nones
c) list of Trues
d) list of Falses
View Answer

Answer: b
Explanation: print() returns None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following Python code snippet?

advertisement
my_string = "hello world"
k = [(i.upper(), len(i)) for i in my_string]
print(k)

a) [(‘HELLO’, 5), (‘WORLD’, 5)]
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)]
c) [(‘HELLO WORLD’, 11)]
d) none of the mentioned
View Answer

Answer: b
Explanation: We are iterating over each letter in the string.
advertisement

4. Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 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 the mentioned
View Answer

Answer: c
Explanation: We have to create an empty list, loop over the contents of the existing list and check if a condition is satisfied before performing some operation and adding it to the new list.

5. What will be the output of the following Python code snippet?

x = [i**+1 for i in range(3)]; print(x);

a) [0, 1, 2]
b) [1, 2, 5]
c) error, **+ is not a valid operator
d) error, ‘;’ is not allowed
View Answer

Answer: a
Explanation: i**+1 is evaluated as (i)**(+1).

6. What will be the output of the following Python code snippet?

print([i.lower() for i in "HELLO"])

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) ‘hello’
c) [‘hello’]
d) hello
View Answer

Answer: a
Explanation: We are iterating over each letter in the string.

7. What will be the output of the following Python code snippet?

print([i+j for i in "abc" for j in "def"])

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
View Answer

Answer: d
Explanation: If it were to be executed as a nested for loop, i would be the outer loop and j the inner loop.

8. What will be the output of the following Python code snippet?

print([[i+j for i in "abc"] for j in "def"])

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
View Answer

Answer: b
Explanation: The inner list is generated once for each value of j.

9. What will be the output of the following Python code snippet?

print([if i%2==0: i; else: i+1; for i in range(4)])

a) [0, 2, 2, 4]
b) [1, 1, 3, 3]
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: Syntax error.

10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
a) [x**-1 for x in [(1, 2, 3)]]
b) [1/x for x in [(1, 2, 3)]]
c) [1/x for x in (1, 2, 3)]
d) error
View Answer

Answer: c
Explanation: x**-1 is evaluated as (x)**(-1).

More MCQs on List Comprehension in Python:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.