Python Questions and Answers – List Comprehension – 2

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

1. Read the information given below carefully and write a list comprehension such that the output is: [ā€˜e’, ā€˜o’]

w="hello"
v=('a', 'e', 'i', 'o', 'u')

a) [x for w in v if x in v]
b) [x for x in w if x in v]
c) [x for x in v if w in v]
d) [x for v in w for x in w]
View Answer

Answer: b
Explanation: The tuple ā€˜v’ is used to generate a list containing only vowels in the string ā€˜w’. The result is a list containing only vowels present in the string ā€œhelloā€. Hence the required list comprehension is: [x for x in w if x in v].
advertisement

2. What will be the output of the following Python code?

[ord(ch) for ch in 'abc']

a) [97, 98, 99]
b) [ā€˜97’, ā€˜98’, ā€˜99’]
c) [65, 66, 67]
d) Error
View Answer

Answer: a
Explanation: The list comprehension shown above returns the ASCII value of each alphabet of the string ā€˜abc’. Hence the output is: [97, 98, 99]. Had the string been ā€˜ABC’, the output would be: [65, 66, 67].
Free 30-Day Java Certification Bootcamp is Live. Join Now!

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

t=32.00
[round((x-32)*5/9) for x in t]

a) [0]
b) 0
c) [0.00]
d) Error
View Answer

Answer: d
Explanation: The value of t in the code shown above is equal to 32.00, which is a floating point value. ā€˜Float’ objects are not iterable. Hence the code results in an error.
advertisement

4. Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
a) [x in range(1, 1000) if x%3==0]
b) [x for x in range(1000) if x%3==0]
c) [x%3 for x in range(1, 1000)]
d) [x%3=0 for x in range(1, 1000)]
View Answer

Answer: b
Explanation: The list comprehension [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3.

5. Write a list comprehension equivalent for the Python code shown below.

for i in range(1, 101):
	if int(i*0.5)==i*0.5:
		print(i)

a) [i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
b) [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
c) [i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
d) [i for i in range(1, 100) if int(i*0.5)=(i*0.5)]
View Answer

Answer: b
Explanation: The code shown above prints the value ā€˜i’ only if it satisfies the condition: int(i*0.5) is equal to (i*0.5). Hence the required list comprehension is: [i for i in range(1, 101) if int(i*0.5)==(i*0.5)].

6. What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))?
a) [1|x for x in [1, 2, 3]]
b) [-1**x for x in [1, 2, 3]]
c) [x**-1 for x in [1, 2, 3]]
d) [x^-1 for x in range(4)]
View Answer

Answer: c
Explanation: The output of the function list(map(lambda x:x**-1, [1, 2, 3])) is [1.0, 0.5, 0.3333333333333333] and that of the list comprehension [x**-1 for x in [1, 2, 3]] is [1.0, 0.5, 0.3333333333333333]. Hence the answer is: [x**-1 for x in [1, 2, 3]].

7. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
a) [(2**x) for x in range(0, 13)]
b) [(x**2) for x in range(1, 13)]
c) [(2**x) for x in range(1, 13)]
d) [(x**2) for x in range(0, 13)]
View Answer

Answer: a
Explanation: The required list comprehension will print the numbers from 1 to 12, each raised to 2. The required answer is thus, [(2**x) for x in range(0, 13)].

8. What is the list comprehension equivalent for?

{x : x is a whole number less than 20, x is even}    (including zero)

a) [x for x in range(1, 20) if (x%2==0)]
b) [x for x in range(0, 20) if (x//2==0)]
c) [x for x in range(1, 20) if (x//2==0)]
d) [x for x in range(0, 20) if (x%2==0)]
View Answer

Answer: d
Explanation: The required list comprehension will print a whole number, less than 20, provided that the number is even. Since the output list should contain zero as well, the answer to this question is: [x for x in range(0, 20) if (x%2==0)].

9. What will be the output of the following Python list comprehension?

[j for i in range(2,8) for j in range(i*2, 50, i)]

a) A list of prime numbers up to 50
b) A list of numbers divisible by 2, up to 50
c) A list of non prime numbers, up to 50
d) Error
View Answer

Answer: c
Explanation: The list comprehension shown above returns a list of non-prime numbers up to 50. The logic behind this is that the square root of 50 is almost equal to 7. Hence all the multiples of 2-7 are not prime in this range.

10. What will be the output of the following Python code?

l=["good", "oh!", "excellent!", "#450"]
[n for n in l if n.isalpha() or n.isdigit()]

a) [ā€˜good’, ā€˜oh’, ā€˜excellent’, ā€˜450’ ]
b) [ā€˜good’]
c) [ā€˜good’, ā€˜#450’]
d) [ā€˜oh!’, ā€˜excellent!’, ā€˜#450’]
View Answer

Answer: b
Explanation: The code shown above returns a new list containing only strings which do not have any punctuation in them. The only string from the list which does not contain any punctuation is ā€˜good’. Hence the output of the code shown above is [ā€˜good’].

Sanfoundry Global Education & Learning Series – Python.

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

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.