Python Program to Split the Array/List and add the First Part to the End

In the previous article, we have discussed Python Program to Count Non Palindrome words in a Sentence
Given a list and a number N the task is to split and move the first N elements of the list to the end in Python.

Examples:

Example1:

Input:

Given List =[11 22 33 44 55 66 77 88 99 111 222 333]
Number =2

Output:

The result part after moving first [ 2 ] numbers to the end is [33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 11, 22]

Example2:

Input:

Given List =[9, 3, 1, 11, 13, 18, 5, 0, 11, 35, 67, 24]
Number =4

Output:

The result part after moving first [ 4 ] numbers to the end is [13, 18, 5, 0, 11, 35, 67, 24, 9, 3, 1, 11]

Program to Split the Array/List and add the First Part to the End in Python

In Python, there are various ways to split the list and add the first portion at the end, some of them are as follows

Method #1: Using Slicing (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the Number N as static input and store it in another variable
  • Calculate the length of the given list and store it in a variable.
  • Slice from N to the length of the list using slicing and store it in a variable say firstpart.
  • Slice from 0 to N using slicing and store it in another variable say secondpart.
  • Add the firstpart and secondpart using the + operator and store the result in another variable say resultpart.
  • Print the resultpart.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlstt = [9, 3, 1, 11, 13, 18, 5, 0, 11, 35, 67, 24]
# Give the Number N as static input and store it in another variable
Numb = 4
# Calculate the length of the given list and store it in a variable.
lengthlst = len(gvnlstt)
# Slice from N to the length of the list using slicing
# and store it in a variable say firstpart.
frstpart = gvnlstt[Numb:lengthlst]
# Slice from 0 to N using slicing and
# store it in another variable say secondpart.
secndpart = gvnlstt[0:Numb]
# Add the firstpart and secondpart using the + operator
# and store the result in another variable say resultpart.
resltpart = frstpart+secndpart
# Print the resultpart.
print('The result part after moving first [',
      Numb, '] numbers to the end is', resltpart)

Output:

The result part after moving first [ 4 ] numbers to the end is [13, 18, 5, 0, 11, 35, 67, 24, 9, 3, 1, 11]

Method #2: Using Slicing (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the Number N as user input using the int(input()) function and store it in another variable
  • Calculate the length of the given list and store it in a variable.
  • Slice from N to the length of the list using slicing and store it in a variable say firstpart.
  • Slice from 0 to N using slicing and store it in another variable say secondpart.
  • Add the firstpart and secondpart using the + operator and store the result in another variable say resultpart.
  • Print the resultpart.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
gvnlstt = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Give the Number N as user input using the int(input())
# function and store it in another variable
Numb = int(input('Enter some random Number N = '))
# Calculate the length of the given list and store it in a variable.
lengthlst = len(gvnlstt)
# Slice from N to the length of the list using slicing
# and store it in a variable say firstpart.
frstpart = gvnlstt[Numb:lengthlst]
# Slice from 0 to N using slicing and
# store it in another variable say secondpart.
secndpart = gvnlstt[0:Numb]
# Add the firstpart and secondpart using the + operator
# and store the result in another variable say resultpart.
resltpart = frstpart+secndpart
# Print the resultpart.
print('The result part after moving first [',
      Numb, '] numbers to the end is', resltpart)

Output:

Enter some random List Elements separated by spaces = 11 22 33 44 55 66 77 88 99 111 222 333
Enter some random Number N = 2
The result part after moving first [ 2 ] numbers to the end is [33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 11, 22]

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.