Zip with Unpack in Python

Level 393 keystrokes

This unpacks a list of tuples into separate tuples using zip with *. The * operator unpacks the pairs for zip.

Python code

# A list of (number, letter) pairs
1pairs = [(1, "a"), (2, "b"), (3, "c")]
# *pairs passes each pair as a separate argument: zip((1, "a"), (2, "b"), (3, "c"))
# zip groups the first items and the second items: (1, 2, 3) and ("a", "b", "c")
2numbers, letters = zip(*pairs)
# Prints (1, 2, 3) ('a', 'b', 'c')
3print(numbers, letters)

Output

(1, 2, 3) ('a', 'b', 'c')
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • ,× 7
  • "× 6
  • (× 5
  • )× 5
  • =× 2
  • [× 1
  • ]× 1
  • *× 1

More Python examples at level 3

← Python typing practice