Thursday, September 25, 2025
HomeLanguagesPython program to randomly create N Lists of K size

Python program to randomly create N Lists of K size

Given a List, the task is to write a Python program to randomly generate N lists of size K.

Examples:

Input : test_list = [6, 9, 1, 8, 4, 7], K, N = 3, 4

Output : [[8, 7, 6], [8, 6, 9], [8, 1, 6], [7, 8, 9]]

Explanation : 4 rows of 3 length are randomly extracted.

Input : test_list = [6, 9, 1, 8, 4, 7], K, N = 2, 3

Output : [[7, 6], [7, 9], [1, 9]]

Explanation : 3 rows of 2 length are randomly extracted.

Method 1 : Using generator + shuffle()

In this, getting random elements is done using shuffle(), and yield with slicing is used to get K size of shuffled list.

Python3




# Python3 code to demonstrate working of
# K sized N random elements
# Using generator + shuffle()
from random import shuffle
 
# get random list
def random_list(sub, K):
    while True:
        shuffle(sub)
        yield sub[:K]
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# initializing K, N
K, N = 3, 4
              
# printing original list
print("The original list is : " + str(test_list))
 
res = []
# getting N random elements
for idx in range(0, N):
    res.append(next(random_list(test_list, K)))
 
# printing result
print("K sized N random lists : " + str(res))


Output:

The original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[7, 1, 8], [8, 6, 1], [4, 9, 6], [6, 9, 1]]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2 : Using product() + sample()

In this, all the possible permutations of K elements are extracted using product(), and from that random sampling of N lists are done.

Python3




# Python3 code to demonstrate working of
# K sized N random elements
# Using product() + sample()
from random import sample
import itertools
 
# initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# initializing K, N
K, N = 3, 4
              
# printing original list
print("The original list is : " + str(test_list))
 
# get all permutations
temp = (idx for idx in itertools.product(test_list, repeat = K))
 
# get Random N from them
res = sample(list(temp), N)
res = list(map(list, res))
 
# printing result
print("K sized N random lists : " + str(res))


Output:

The original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[1, 1, 1], [6, 9, 4], [8, 7, 6], [4, 8, 8]]

Time Complexity: O(n) where n is the number of elements in the list “test_list”. The product() + sample() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list 

Method 3: Using combinations() and randint()

We can use the combinations() function from the itertools module to generate all possible combinations of K elements from the input list, and then use the randint() function from the random module to select N random combinations.

 steps to implement this approach:

  1. Import the required modules:
  2. Define the function to generate K sized N random lists:
  3. Call the function with the input list, K and N:

Python3




from itertools import combinations
from random import randint
 
def random_lists(lst, K, N):
    combos = list(combinations(lst, K))
    rand_combos = [combos[randint(0, len(combos) - 1)] for i in range(N)]
    return rand_combos
 
test_list = [6, 9, 1, 8, 4, 7]
K, N = 3, 4
 
res = random_lists(test_list, K, N)
 
print("K sized N random lists : " + str(res))


Output

K sized N random lists : [(9, 1, 8), (6, 8, 7), (6, 9, 4), (1, 8, 4)]

Time complexity: O(1)
Auxiliary space: O(N)

Method 5: Using random.sample() and slicing

  1. Import random module to use random.sample() method.
  2. Initialize the list of integers test_list.
  3. Initialize variables K and N.
  4. Print the original list.
  5. Use random.sample() method to get a random sample of K integers from test_list.
  6. Repeat the above step N times using a loop and append the result to a list.
  7. Print the final result.

Python3




import random
 
# Initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# Initializing K, N
K, N = 3, 4
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Getting Random N lists of size K
res = []
for i in range(N):
    res.append(random.sample(test_list, K))
 
# Printing result
print("K sized N random lists : " + str(res))


Output

The original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[4, 6, 8], [1, 7, 4], [6, 9, 1], [1, 8, 7]]

Time Complexity: O(NK)
Auxiliary Space: O(NK)

Method 6: Using NumPy library

Python3




import numpy as np
 
# Initializing list
test_list = [6, 9, 1, 8, 4, 7]
 
# Initializing K, N
K, N = 3, 4
 
# Printing original list
print("The original list is: " + str(test_list))
 
# Get Random N lists of size K using NumPy
arr = np.array(test_list)
np.random.shuffle(arr)  # Shuffle the array in-place
 
# Adjust the number of elements in arr to ensure equal division
num_elements = N * K
if num_elements > len(arr):
    arr = np.tile(arr, num_elements // len(arr) + 1)[:num_elements]
 
res = np.split(arr, N)
 
# Converting NumPy arrays to nested lists
res = [sublist.tolist() for sublist in res]
 
# Printing result
print("K-sized N random lists: " + str(res))


Output

The original list is : [6, 9, 1, 8, 4, 7]
K sized N random lists : [[4, 6, 8], [1, 7, 4], [6, 9, 1], [1, 8, 7]]

Time Complexity: O(N * K) since we still shuffle the array once and take the first N * K elements.
Auxiliary Space: O(N * K) as we need to store the shuffled array and the N subarrays.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6754 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS