Friday, December 27, 2024
Google search engine
HomeLanguagesPython program to convert a list into a list of lists using...

Python program to convert a list into a list of lists using a step value

Given a List, the task here is to write a python program that can split the list into list of lists using by a step value here denoted via K.

Input : test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8], K = 3
Output : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]
Explanation : 5, 2 and 9 are 0th, 3rd and 6th element respectively, and hence ( difference 3 ) grouped together.

Input : test_list = [5, 6, 3, 2, 7, 1], K = 3
Output : [[5, 2], [6, 7], [3, 1]]
Explanation : 5 and 2 are 0th and 3rd element respectively, and hence ( difference 3 ) grouped together.

Method 1: Using loop and slicing

In this, loop is employed to skip numbers as required, and each subsequent skipped sliced list is extracted using slicing and is appended to result.

Example:

Python3




# initializing list
test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing skips
K = 3
 
res = []
for idx in range(0, K):
 
    # 3rd arg. of slicing skips by K
    res.append(test_list[idx::K])
 
# printing result
print("Stepped splitted List : " + str(res))


Output

The original list is : [5, 6, 3, 2, 7, 1, 9, 10, 8]
Stepped splitted List : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]

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

Method 2: Using list comprehension and slicing

Similar to above method, only difference being usage of list comprehension for task of iteration instead of loop for shorthand alternative.

Step-by-step approach:

  • Initialize a variable called K with the value 3.
  • Use a list comprehension to create a new list called res that contains sublists of test_list. The sublist at index i in res is created by selecting every Kth element starting from index i in test_list.
  • Print the resulting list.

Example:

Python3




# initializing list
test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing skips
K = 3
 
# list comprehension used as one liner
res = [test_list[idx::K] for idx in range(0, K)]
 
# printing result
print("Stepped splitted List : " + str(res))


Output:

The original list is : [5, 6, 3, 2, 7, 1, 9, 10, 8]
Stepped splitted List : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]

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

Method 4: Using numpy array slicing

  • Import the numpy library.
  • Initialize the input list.
  • Convert the input list to a numpy array.
  • Initialize the skips variable.
  • Use numpy array slicing to split the array into K chunks.
  • Convert the resulting arrays back to Python lists.
  • Store the sliced lists in the “res” variable.
  • Print the resulting “res” variable.

Python3




import numpy as np
 
# initializing list
test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8]
 
# convert list to numpy array
arr = np.array(test_list)
 
# initializing skips
K = 3
 
# use numpy array slicing to split the array
res = [arr[i::K] for i in range(K)]
 
# convert the resulting arrays back to lists
res = [list(x) for x in res]
 
# printing result
print("Stepped splitted List : " + str(res))


Output: 

Stepped splitted List : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list. This is because we need to store the sliced arrays in memory as separate lists.

Method 5: Using a list comprehension and the enumerate() function:

Python3




# initializing list
test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing skips
K = 3
 
# splitting the list using list comprehension and enumerate function
res = [[test_list[i] for i in range(len(test_list)) if idx == i % K] for idx, val in enumerate(test_list)]
 
# printing result
print("Stepped splitted List : " + str(res[:K]))


Output

The original list is : [5, 6, 3, 2, 7, 1, 9, 10, 8]
Stepped splitted List : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]

Time complexity: O(N), where N is the length of the given list. 
Auxiliary space: O(N/K), which is the space required to store the sliced sublists.

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

Most Popular

Recent Comments