Skip to content

Instantly share code, notes, and snippets.

@sungiven
Created January 27, 2024 18:54
Show Gist options
  • Save sungiven/9243f6d82f8848eeb7ac4c04ac990bdf to your computer and use it in GitHub Desktop.
Save sungiven/9243f6d82f8848eeb7ac4c04ac990bdf to your computer and use it in GitHub Desktop.
Cartesian product of card ranks & suits
#!/usr/bin/env python3
card_ranks = ["A", "K", "Q"]
card_suits = ["♠", "♥", "♦", "♣"]
# cartesian product of cards, arranged by ranks
cards = [(rank, suit) for rank in card_ranks for suit in card_suits]
print(cards)
# cartesian product of cards, arranged by suits
cards = [(rank, suit) for suit in card_suits for rank in card_ranks]
print(cards)
"""
output:
[('A', '♠'), ('A', '♥'), ('A', '♦'), ('A', '♣'), ('K', '♠'), ('K', '♥'), ('K', '♦'), ('K', '♣'), ('Q', '♠'), ('Q', '♥'), ('Q', '♦'), ('Q', '♣')]
[('A', '♠'), ('K', '♠'), ('Q', '♠'), ('A', '♥'), ('K', '♥'), ('Q', '♥'), ('A', '♦'), ('K', '♦'), ('Q', '♦'), ('A', '♣'), ('K', '♣'), ('Q', '♣')]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment