Program to split the array and add the first part to the end using Python

Introduction

In this section, we will understand the python code to split the given array and add the first part of the array to the end.

Program to split the array and add the first part to the end using Python

Program

def split(arr, k):
    arr = arr[k:] + arr[:k]
    return arr
   
k = 2
arr = [10, 13, 5, 17]
print("Output array is", split(arr, k))

Output

Program to split the array and add the first part to the end using Python Output

Explanation

In Python programming, we can easily slice the string, list, tuple. The sliced operation results in the new desired output. For example, we have a string

>> input = ‘word’

>> print( input[:2] )                          # slice the string at position 2

‘wo’       ( The output contains the character from start to 2 (excluded) )

Similarly in the above code, we have created a function split() which will split the array at the given position (2) and add the start to the end. The output array is returned and displayed using the print function.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.