Important Python Concept

Python Program to Shuffle Deck of Cards

We will learn how to shuffle a deck of cards using various python libraries. For this, we will prefer to use the random library built in python language.

import itertools
import random

# let's get a deck of cards
k = list(itertools.product(range(1, 14), ['Diamond', 'Heart', 'Spade', 'Heart']))
random.shuffle(k)  # shuffle it properly

print("Shuffled set:")
for i in range(5):
    print(k[i][0], "of", k[i][1])


Shuffled set:
1 of Diamond
10 of Diamond
4 of Heart
3 of Spade
3 of Heart


In this example, we have imported some important libraries which are itertools and random.

Here, we have declared a list and stored it in a variable k. Then, in the list function, we have used itertools which is basically used to iterate over the data structure, in this case, we have traded through each element present in the product range of 1 to 14. 

After this, we have defined four different strings which will be helpful to shuffle.

Then, we used a random library with a shuffle function where we are passing the variable k(list). This function is used to shuffle the list.

Finally, we are printing the list in the range of 5 numbers. Now we can see the output shuffled. Note: every time you execute this program, you will receive a new set of shuffled lists.


Here, we have successfully learned how to shuffle a deck of cards.

Aniket Malik

Aniket Malik


Facing difficulty with

this concept?