Built-ins & Standard Library · Question 12 · Medium

zip is used with the * operator to split a list of pairs. What does this code print?

pairs = [("a", 1), ("b", 2), ("c", 3)]
letters, nums = zip(*pairs)
print(letters, sum(nums))
  1. ('a', 'b', 'c') 6
  2. ['a', 'b', 'c'] 6
  3. ('a', 1) 6
  4. ValueError
Answer and explanation

Answer: A. ('a', 'b', 'c') 6

Explanation: zip(*pairs) passes the three pairs as separate arguments, so zip groups the first items together and the second items together. Each group is a tuple: ('a', 'b', 'c') and (1, 2, 3), whose sum is 6.

Previous: max is called on a dictionary of city temperatures, once plainly and once with… · Next: A file tools.py ends with a block under if __name__ == "__main__":. When does…

Practise built-ins & standard library

Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.

More built-ins & standard library questions

More python practice

Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge