ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import itertools
g = itertools.groupby('aab')
k, grp = next(g)
next(g)
print(k, list(grp))- A. a ['a', 'a']
- B. a ['a']
- C. None []
- D. a []
Correct answer: D. a []
groupby shares one underlying iterator. Advancing to the next group with next(g) skips the rest of the first group, so the earlier group iterator is empty when read afterwards.
More Python Programming questions
- What is printed? python import itertools g = iter(range(10)) print(list(itertools.islice(g, 2, 7, 2)), next(g))
- What is printed? python import itertools print(list(itertools.ziplongest('ab', 'xyz', fillvalue='-')))
- What is printed? python import itertools print(sum(a < b for a, b in itertools.pairwise([1, 3, 2, 5, 4, 6])))
- What is printed? python import itertools print(list(itertools.batched(range(7), 3))[-1])
- What is printed? python import sys print(sys.getrecursionlimit())
- What is printed? python import sys sys.setrecursionlimit(50) def f(n): return 0 if n == 0 else 1 + f(n - 1) try: print(f(60)) except…