ProgrammingHardProduct Company SDE · Python internals
What does the following Python 3 code print? print([i for i in range(5) if i != 2][::-1])
- A. [0, 1, 3, 4]
- B. [0, 1, 2, 3, 4]
- C. [4, 3, 2, 1, 0]
- D. [4, 3, 1, 0]
Correct answer: D. [4, 3, 1, 0]
The comprehension first builds [0,1,3,4] (skipping 2), then [::-1] reverses that filtered list.
More Python Programming questions
- What does the following Python 3 code print? matrix = [[1, 2], [3, 4], [5, 6]] transposed = [[row[i] for row in matrix] for i in range(2)]…
- What does the following Python 3 code print? print(list(enumerate(["a", "b", "c"], start=1)))
- What does the following Python 3 code print? print(dict(enumerate(["x", "y", "z"])))
- What does the following Python 3 code print? a = {1, 2, 3} b = {2, 3, 4} print(a & b, a | b, a - b, a ^ b)
- What does the following Python 3 code print? a = frozenset([1, 2, 3]) try: a.add(4) except AttributeError: print("AttributeError")
- What does the following Python 3 code print? s = set([1, 2, 2, 3, 3, 3]) print(sorted(s), len(s))