Skip to content
the poor coder

divide array into parts python

Poor Man
Published
Reading time
2 min read
On this page

Divide Array into Parts in Python#

Dividing arrays into parts is a common task in programming. In Python, there are different ways to achieve this. Here are some ways to divide an array into parts:

Method 1: Using Slicing#

One way to divide an array into parts in Python is by using slicing. Slicing is a way to extract a portion of a string, list, or tuple.

Text

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3
result = []

for i in range(0, len(array), n):
    result.append(array[i:i + n])
    
print(result)

In this code, we first define an array and the number of parts we want to divide it into. We then create an empty list called "result" to store the divided parts. We use a for loop to iterate through the array and slice it into parts using the range function. We use the append method to add each sliced part to the "result" list. Finally, we print the "result" list.

Method 2: Using numpy.array_split()#

Another way to divide an array into parts in Python is by using the numpy library's array_split() function.

Text

import numpy as np

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3
result = np.array_split(array, n)

print(result)

In this code, we first import the numpy library. We then define an array and the number of parts we want to divide it into. We use the array_split() function to divide the array into parts and store the result in the "result" variable. Finally, we print the "result" variable.

Method 3: Using List Comprehension#

Another way to divide an array into parts in Python is by using list comprehension.

Text

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3
result = [array[i:i + n] for i in range(0, len(array), n)]

print(result)

In this code, we first define an array and the number of parts we want to divide it into. We use list comprehension to slice the array into parts using the range function. Finally, we print the "result" list.

Overall, these are three different methods to divide an array into parts in Python. Depending on the context and requirements of your program, one method may be more suitable than others.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC