{"id":18181,"date":"2021-08-26T10:42:26","date_gmt":"2021-08-26T05:12:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=18181"},"modified":"2021-11-22T18:37:12","modified_gmt":"2021-11-22T13:07:12","slug":"python-program-to-split-the-array-list-and-add-the-first-part-to-the-end","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-split-the-array-list-and-add-the-first-part-to-the-end\/","title":{"rendered":"Python Program to Split the Array\/List and add the First Part to the End"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count Non Palindrome words in a Sentence<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n In Python, there are various ways to split the list and add the first portion at the end, some of them are as follows<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\n
\nGiven 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.<\/p>\nGiven List =[11 22 33 44 55 66 77 88 99 111 222 333]\r\nNumber =2<\/pre>\n
The result part after moving first [ 2 ] numbers to the end is [33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 11, 22]<\/pre>\n
Given List =[9, 3, 1, 11, 13, 18, 5, 0, 11, 35, 67, 24]\r\nNumber =4<\/pre>\n
The result part after moving first [ 4 ] numbers to the end is [13, 18, 5, 0, 11, 35, 67, 24, 9, 3, 1, 11]<\/pre>\n
Program to Split the Array\/List and add the First Part to the End in Python<\/h2>\n
\n
Method #1: Using Slicing (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvnlstt = [9, 3, 1, 11, 13, 18, 5, 0, 11, 35, 67, 24]\r\n# Give the Number N as static input and store it in another variable\r\nNumb = 4\r\n# Calculate the length of the given list and store it in a variable.\r\nlengthlst = len(gvnlstt)\r\n# Slice from N to the length of the list using slicing\r\n# and store it in a variable say firstpart.\r\nfrstpart = gvnlstt[Numb:lengthlst]\r\n# Slice from 0 to N using slicing and\r\n# store it in another variable say secondpart.\r\nsecndpart = gvnlstt[0:Numb]\r\n# Add the firstpart\u00a0and secondpart\u00a0using the + operator\r\n# and store the result in another variable say resultpart.\r\nresltpart = frstpart+secndpart\r\n# Print the resultpart.\r\nprint('The result part after moving first [',\r\n Numb, '] numbers to the end is', resltpart)\r\n<\/pre>\n
The result part after moving first [ 4 ] numbers to the end is [13, 18, 5, 0, 11, 35, 67, 24, 9, 3, 1, 11]<\/pre>\n
Method #2: Using Slicing (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\ngvnlstt = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Give the Number N as user input using the int(input())\r\n# function and store it in another variable\r\nNumb = int(input('Enter some random Number N = '))\r\n# Calculate the length of the given list and store it in a variable.\r\nlengthlst = len(gvnlstt)\r\n# Slice from N to the length of the list using slicing\r\n# and store it in a variable say firstpart.\r\nfrstpart = gvnlstt[Numb:lengthlst]\r\n# Slice from 0 to N using slicing and\r\n# store it in another variable say secondpart.\r\nsecndpart = gvnlstt[0:Numb]\r\n# Add the firstpart\u00a0and secondpart\u00a0using the + operator\r\n# and store the result in another variable say resultpart.\r\nresltpart = frstpart+secndpart\r\n# Print the resultpart.\r\nprint('The result part after moving first [',\r\n Numb, '] numbers to the end is', resltpart)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 11 22 33 44 55 66 77 88 99 111 222 333\r\nEnter some random Number N = 2\r\nThe result part after moving first [ 2 ] numbers to the end is [33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 11, 22]<\/pre>\n
\n