Itertools Permutations in Python

Level 3109 keystrokes

This generates all 2-element permutations where order matters.

Python code

# permutations() picks items where order matters
1from itertools import permutations
2
# Three items to arrange
3items = ["A", "B", "C"]
# Every ordered pair of 2 different items: 3 × 2 = 6 tuples
# ("A", "B") and ("B", "A") are both included
4perms = list(permutations(items, 2))
# Prints [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
5print(perms)

Output

[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
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.

  • "× 6
  • ,× 3
  • (× 3
  • )× 3
  • =× 2
  • [× 1
  • ]× 1

More Python examples at level 3

← Python typing practice