10-Jul-2021
.
Admin
Hi Guys,
Now let's see example of how to split the array and add the first part to the end. We will talk about python program to split the array and add the first part to the end. I am going to show you an array and split it from a specified position, and move the first part of array add to the end.
Here i will give you example two example of python program to split the array and add the first part to the end So let's see below example.
Example 1
# Python program to split array
def splitArr(arr, n, k):
for i in range(0, k):
x = arr[0]
for j in range(0, n-1):
arr[j] = arr[j + 1]
arr[n-1] = x
arr = [1,2,3,4,5,6,7,8,9,10,11,12]
n = len(arr)
position = 3
splitArr(arr, n, position)
for i in range(0, n):
print(arr[i], end = ' ')
Output
4,5,6,7,8,9,10,11,12
Example 2
# Python program to split array and move first
def splitArr(a, n, k):
b = a[:k]
return (a[k::]+b[::])
arr = [1,2,3,4,5,6,7,8,9,10,11,12]
n = len(arr)
position = 2
arr = splitArr(arr, n, position)
for i in range(0, n):
print(arr[i], end = ' ')
Output
3,4,5,6,7,8,9,10,11,12
It will help you....
#Python