-
What is the output of the following code?
[x for x in range(5)]
- [0, 1, 2, 3, 4]
- [1, 2, 3, 4, 5]
- [0, 2, 4]
- [1, 3, 5]
-
Which of the following is the correct syntax for a list comprehension?
- (expression for item in iterable)
- {expression for item in iterable}
- [expression in item for iterable]
- [expression for item in iterable]
-
What will be the output of this code?
[x*2 for x in range(5) if x % 2 == 0]
- [0, 1, 2, 3, 4]
- [0, 2, 4, 6, 8]
- [0, 4, 8]
- [2, 6]
-
What does 'iterable' represent in a list comprehension?
- A condition to filter elements.
- A sequence (like a list or range) to iterate over.
- An expression to be evaluated.
- A function to apply to each element.
-
Which of the following list comprehensions squares only the odd numbers in the range 0-9?
- [x2 for x in range(10)]
- [x for x in range(10) if x % 2 != 0]
- [x2 for x in range(10) if x % 2 == 0]
- [x2 for x in range(10) if x % 2 != 0]
-
What is the primary benefit of using list comprehension over traditional loops?
- Increased code complexity
- Improved readability and conciseness
- Reduced memory usage
- Enhanced debugging capabilities
-
What will the following code output?
[ (x, y) for x in [1,2,3] for y in [4,5,6]]
- [(1, 4), (2, 5), (3, 6)]
- [(1, 2, 3), (4, 5, 6)]
- [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
- Error