Skip to content

Tuple creation, unpacking, and ordering [ref]

Problem

  • Create a one-element tuple containing 5.
  • Unpack (2, 7) into start and end.
  • Compare (2, 3) with (2, 5).
  • Sort [(2, "b"), (1, "z"), (2, "a")] naturally.
  • Prepend "word" to ("one", "two") using unpacking.

Python solution

(5,)  # (5,)

start, end = (2, 7)
(start, end)  # (2, 7)

(2, 3) < (2, 5)  # True

sorted([(2, "b"), (1, "z"), (2, "a")])
# [(1, 'z'), (2, 'a'), (2, 'b')]

suffix = ("one", "two")
("word", *suffix)  # ('word', 'one', 'two')