ProgrammingHardTCS NQT · Wipro pattern
Which statement about itertools.groupby is correct?
- A. It groups only consecutive items that have equal keys
- B. It sorts its input before grouping
- C. It returns a dictionary of lists
- D. It works only on lists and not on other iterables
Correct answer: A. It groups only consecutive items that have equal keys
groupby walks the input once and starts a new group whenever the key changes, so unsorted input gives repeated groups. It does not sort, build a dict, or require a list.
More Python Programming questions
- What is printed? python print(-1 >> 1, -8 >> 2, 1 << 70 > 1020, (-5) & 0xF)
- What is printed? python print(0 or [] or 'x', 2 and 0 and 5, None or 0)
- What is printed? python d = {} d.setdefault('k', []).append(1) d.setdefault('k', []).append(2) print(d, d.setdefault('j', 9), 'j' in d)
- What is printed? python w = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(w, key=lambda s: (len(s), s)), sorted(w, key=len…
- What is printed? python print(bool(0.0), bool(' '), bool(None is None), bool(float('nan')))
- What is printed? python def findmin(a): lo, hi = 0, len(a) - 1 while lo < hi: mid = (lo + hi) // 2 if a[mid] > a[hi]: lo = mid + 1 else…