Well done!
You have completed Understanding and Utilizing List Comprehensions in Python Quiz!
Quiz Question 1 of 5
Consider the following code:
input_list = [5, 'cat', 12, 'dog', 0]
output_list = [str(i).upper() if isinstance(i, str) else i**2 for i in input_list]
What would be the correct output of output_list
and what does it indicate about the flexibility of list comprehensions in handling mixed data types?
Choose the correct answer below:
-
A
[25, 'CAT', 144, 'DOG', 0]
, indicating that list comprehensions cannot handle mixed data types without explicit type-checking. -
B
['25', 'CAT', '144', 'DOG', '0']
, indicating that list comprehensions apply string operations to all elements regardless of their original type. -
C
[25, 'cat', 144, 'dog', 0]
, indicating that list comprehensions automatically convert all elements to strings. -
D
[25, 'CAT', 144, 'DOG', 0]
, indicating that list comprehensions can handle mixed data types with conditional expressions.