For the first question:

A = [ [1,2,3,4,5,6,7,8] for i in range(8)]
n = len(A[0])
x = int(n/2)

TEMP = [[None]*2 for i in range(2)]

for w in range(2):
    for q in range(2):
        TEMP[w][q] = [item[q * x:(q * x) + x] for item in A[w * x:(w * x) + x]]

for w in range(2):
    for q in range(2):
        print("{i}, {j}: {item}".format(i=w, j=q, item=repr(TEMP[w][q])))
Answer from Salah Eddine Lahniche on Stack Overflow
Discussions

python - Creating an array without numpy - Stack Overflow
In my homework, numpy usage wasn't allowed but I realize that just now. I have to delete all the np.array() components and define an array without using them. I couldn't find a way. For example, I ... More on stackoverflow.com
🌐 stackoverflow.com
Help with matrices (without using numpy)
If A is a matrix (2D list), len(A) will be the # of rows, and len(a row) will be the # of columns. Use that to get (and compare) dimensions. AB(i,j) = (row i of A) dot (col j of B) Suppose my two matrices (A and B) are [[1,2], [[1,2,3], [3,4] and [4,5,6]] and you want the (0-based indexing) entry in row 1, col 2 that comes from [3,4] dot [3,6]. What do the indices of A look like in that row? What do the indices of B look like in that column? More on reddit.com
🌐 r/learnpython
6
8
February 19, 2021
Creating a Matrix in Python without numpy - Stack Overflow
I'm trying to create and initialize a matrix. Where I'm having an issue is that each row of my matrix I create is the same, rather than moving through the data set. I've tried to correct it by che... More on stackoverflow.com
🌐 stackoverflow.com
Creating an array without using numpy
In my homework, numpy usage wasn't allowed but I realize that just now. I have to delete all the np.array() components and define an array without… More on reddit.com
🌐 r/learnpython
4
0
May 29, 2021
🌐
Quora
quora.com › How-do-I-slice-a-2D-array-on-Python-without-using-NumPy
How to slice a 2D array on Python without using NumPy - Quora
Answer (1 of 2): Horizontal slicing is possible, but for vertical slicing you’ll need NumPy for it. Here’s the code and make sure you follow the comments:- [code]a = [[1,2,3],[4,5,6],[7,8,9]] #(1)Horizontal rows r1 = a[0][:] r2 = a[1][:] r3 = a[2][:] #(2)For specific value required slicing ...
🌐
Medium
medium.com › @Evelyn.Taylor › split-an-array-into-smaller-arrays-of-a-specific-size-e181537105c8
Split an Array into Smaller Arrays of a Specific Size | by Evelyn Taylor | Medium
July 4, 2023 - One straightforward approach to splitting an array into smaller arrays is by using a loop. We can iterate over the original array and extract a specific number of elements in each iteration to form a new smaller array. Here’s an example in Python:
🌐
Reddit
reddit.com › r/learnpython › help with matrices (without using numpy)
r/learnpython on Reddit: Help with matrices (without using numpy)
February 19, 2021 -

Hello,

I'm learning to code in Python and I'm stuck on a part of a question. I have googled a lot and tried to do it without success.

The user enters two matrices that are retained in the program as two-dimensional lists. The program checks whether the matrix sizes allow matrix multiplication and in that case performs the matrix multiplication. The result is saved in a new two-dimensional list.

I succeed with the part where users input the lists, but do not know how to proceed after that. Does anyone have any ideas on how to do this? I'm not allowed to use numpy on this assignment.

I would really appreciate some help.

Top answer
1 of 2
10

You need to keep track of the current index in your loop.

Essentially you want to turn a list like 0,1,2,3,4,....24 (these are the indices of your initial array, alpha) into:

R1C1, R1C2, R1C3, R1C4, R1C5 R2C1, R2C2... etc

I added the logic to do this the way you are currently doing it:

def createMatrix(rowCount, colCount, dataList):
    mat = []
    for i in range(rowCount):
        rowList = []
        for j in range(colCount):
            # you need to increment through dataList here, like this:
            rowList.append(dataList[rowCount * i + j])
        mat.append(rowList)

    return mat

def main():
    alpha = ['a','b','c','d','e','f','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    mat = createMatrix(5,5,alpha)
    print (mat)

main()

which then prints out:

[['a', 'b', 'c', 'd', 'e'], ['f', 'h', 'i', 'j', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z']]

The reason you were always receiving a,b,c,d,e is because when you write this:

        rowList.append(dataList[j])

what it is effectively doing is it is iterating 0-4 for every row. So basically:

i = 0
rowList.append(dataList[0])
rowList.append(dataList[1])
rowList.append(dataList[2])
rowList.append(dataList[3])
rowList.append(dataList[4])
i = 1
rowList.append(dataList[0]) # should be 5
rowList.append(dataList[1]) # should be 6
rowList.append(dataList[2]) # should be 7
rowList.append(dataList[3]) # should be 8
rowList.append(dataList[4]) # should be 9

etc.

2 of 2
7

You can use a list comprehension:

>>> li= ['a','b','c','d','e','f','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
>>> [li[i:i+5] for i in range(0,len(li),5)]
[['a', 'b', 'c', 'd', 'e'], ['f', 'h', 'i', 'j', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z']]

Or, if you don't mind tuples, use zip:

>>> zip(*[iter(li)]*5)
[('a', 'b', 'c', 'd', 'e'), ('f', 'h', 'i', 'j', 'k'), ('l', 'm', 'n', 'o', 'p'), ('q', 'r', 's', 't', 'u'), ('v', 'w', 'x', 'y', 'z')]

Or apply list to the tuples:

>>> map(list, zip(*[iter(li)]*5))
[['a', 'b', 'c', 'd', 'e'], ['f', 'h', 'i', 'j', 'k'], ['l', 'm', 'n', 'o', 'p'], ['q', 'r', 's', 't', 'u'], ['v', 'w', 'x', 'y', 'z']]
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.split.html
numpy.split — NumPy v2.5 Manual
If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along axis the array is split.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.split.html
numpy.split — NumPy v2.6.dev0 Manual
If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along axis the array is split.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-split-the-array-and-add-the-first-part-to-the-end
Python Program to Split the Array and Add First Part to the End - GeeksforGeeks
November 11, 2025 - Input: arr = [12, 10, 5, 6, 52, 36], k = 2 Output: [5, 6, 52, 36, 12, 10] Explanation: Split the array at index k and move the first part [12, 10] (for k = 2) to the end. Below are the different methods to perform this task: deque from the collections module allows efficient rotation of elements. Its rotate() method handles both left and right rotations internally without explicit shifting.
Author: array-split
🌐
Reddit
reddit.com › r/learnpython › creating an array without using numpy
r/learnpython on Reddit: Creating an array without using numpy
May 29, 2021 - import numpy as np def rotate_clockwise(x): return x[::-1].T def find(element, matrix): for i in range(len(matrix)): for j in range(len(matrix[i])): if matrix[i][j] == element: return (i+1, j+1) N = int(input()) S = int(input()) arr = np.array(range(0,N*N)) arr.shape = N,N for i in range(S): a,b,c = [int(x) for x in input().split()] arr[a - 1:a + c, b - 1:b + c] = rotate_clockwise(arr[a - 1:a + c, b - 1:b + c]) M = int(input()) items = np.array(range(0,M)) for i in range(M): danscisayisi=int(input()) items[i]=(danscisayisi) for i in range(0, len(items)): items[i] = int(items[i]) noktalar = np.array(range(0,M)) for i in range(M): coord=(find(items[i],arr)) result = " ".join(str(x) for x in coord) print(result)
🌐
W3Schools
w3schools.com › python › ref_string_split.asp
Python String split() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The split() method splits a string into a list.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_split.asp
NumPy Splitting Array
We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.array_split(arr, 3) print(newarr) Try it Yourself »
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Split an array with np.split, np.vsplit, np.hsplit, etc. | note.nkmk.me
February 6, 2024 - Specifying a list of integers as the second argument, indices_or_sections, splits the array at those index positions, with indexing starting at 0.
🌐
DataCamp
datacamp.com › doc › numpy › split
NumPy split()
The `split()` function in NumPy is used to divide an array into multiple sub-arrays.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › split
Python Numpy split() - Divide Array | Vultr Docs
January 1, 2025 - The split() function in the NumPy library is a versatile tool for dividing an array into multiple sub-arrays. Whether working with large datasets or performing parallel computations, this function allows for efficient data manipulation by segmenting arrays based on specified conditions.
🌐
Imperial College London
python.pages.doc.ic.ac.uk › lessons › numpy › 05-manipulation › 03-split.html
Introduction to NumPy and Matplotlib > Array split | Python Programming | Department of Computing | Imperial College London
>>> x = np.arange(1, 19).reshape((2, 9)) >>> print(x) [[ 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18]] >>> y = np.split(x, 3, axis=1) # split on axis 1 into 3 evenly-sized sub-arrays >>> print(y[0]) [[ 1 2 3] [10 11 12]] >>> print(y[1]) [[ 4 5 6] [13 14 15]] >>> print(y[2]) [[ 7 8 9] [16 17 18]] >>> y = np.split(x, 2, axis=0) # split on axis 0 into 2 evenly-sized sub-arrays >>> print(y[0]) [[1 2 3 4 5 6 7 8 9]] >>> print(y[1]) [[10 11 12 13 14 15 16 17 18]] >>> y = np.split(x, 4, axis=1) # attempt to split on axis 1 into 4 evenly-sized sub-arrays ValueError: array split does not result in an equal division · There is a similar function np.array_split() that allows you to split an array without needing to be strictly even.