Python Questions and Answers – List Comprehension – 2

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

Q 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 v if w in v]
C. [x for x in w if x in v]
D. [x for v in w for x in w]

Show Answer Answer:-C. [x for x in w if x in v]
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].

Q 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

Show Answer Answer:-A. [97, 98, 99]
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].

Q 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

Show Answer Answer:-D. Error
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.

Q 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)]

Show Answer Answer:-B. [x for x in range(1000) if x%3==0]
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.

Q 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)]

Show Answer Answer:-C. [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
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)].

Q 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)]

Show Answer Answer:-C. [x**-1 for x in [1, 2, 3]]
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]].

Q 7. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
A. [(x**2) for x in range(1, 13)]
B. [(2**x) for x in range(0, 13)]
C. [(2**x) for x in range(1, 13)]
D. [(x**2) for x in range(0, 13)]

Show Answer Answer:-B. [(2**x) for x in range(0, 13)]
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)].

Q 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)]

Show Answer Answer:-D. [x for x in range(0, 20) if (x%2==0)]
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)].

Q 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

Show Answer Answer:-C. A list of non prime numbers, up to 50
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.

Q 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’]
B. [‘good’, ‘oh’, ‘excellent’, ‘450’ ]
C. [‘good’, ‘#450’]
D. [‘oh!’, ‘excellent!’, ‘#450’]

Show Answer Answer:-A. [‘good’]
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’].

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page