ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
print(list(zip([1, 2, 3], [4, 5], [6, 7, 8, 9]))) from itertools import zip_longest print(list(zip_longest([1, 2, 3], [4, 5], fillvalue=0)))
- A. [(1, 4, 6), (2, 5, 7)] [(1, 4), (2, 5), (3, 0)]
- B. [(1, 4, 6), (2, 5, 7), (3, None, 8)] [(1, 4), (2, 5), (3, 0)]
- C. [(1, 4, 6), (2, 5, 7)] [(1, 4), (2, 5), (3, None)]
- D. [(1, 4, 6), (2, 5, 7), (3, None, 8)] [(1, 4), (2, 5)]
Correct answer: A. [(1, 4, 6), (2, 5, 7)] [(1, 4), (2, 5), (3, 0)]
zip() stops at the shortest iterable (length 2 here), giving 2 tuples. zip_longest pads the shorter [4,5] out to length 3 with fillvalue=0, giving (3, 0) as the final tuple.
More Python Programming questions
- What is printed? python print(list(enumerate(["a", "b", "c"], start=5)))
- What is printed? python from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass try: s = Shape() except…
- What is printed? python class Meta(type): def new(mcs, name, bases, ns): print("creating", name) return super().new(mcs, name, bases, ns)…
- What is printed? python class Vec: def init(self, x, y): self.x, self.y = x, y def radd(self, o): return Vec(self.x + o, self.y + o) def…
- What is printed? python class Idx: def index(self): return 3 l = [0, 1, 2, 3, 4, 5] print(l[Idx()])
- What is printed? python def f(a, b={"x": 1}): b["x"] += a return b print(f(1)) print(f(2))