{"id":24734,"date":"2021-11-02T09:41:56","date_gmt":"2021-11-02T04:11:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=24734"},"modified":"2021-11-05T17:20:50","modified_gmt":"2021-11-05T11:50:50","slug":"python-list-insert-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-insert-method-with-examples\/","title":{"rendered":"Python List insert() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List copy() Method with Examples<\/a> Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n List insert() Method in Python:<\/strong><\/p>\n Inserts the specified value at the specified position using the insert() method.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n position:<\/strong> This is Required. A number indicating where the value should be inserted.<\/p>\n element:<\/strong> This is Required. Any type of element (string, number, object etc.)<\/p>\n Return Value:<\/strong><\/p>\n The insert() method produces no results; it returns None. It only makes changes to the current list.<\/p>\n 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 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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples<\/a> and use them.<\/p>\n In the previous article, we have discussed Python List copy() Method with Examples List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of …<\/p>\n
\nList in Python:<\/strong><\/p>\nlist.insert(position, element)<\/pre>\n
Given List = [\"hello\", \"is\", \"btechgeeks\"]\r\nGiven position = 1\r\nGiven element = \"this\"<\/pre>\n
The given list after inserting the given element: \r\n['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
Given List = [\"good\", \"morning\", \"btechgeeks\"]\r\nGiven position = 1\r\nGiven Tuple = (1, 3, 5, 7)<\/pre>\n
The given list after inserting the given Tuple: \r\n['good', (1, 3, 5, 7), 'morning', 'btechgeeks']<\/pre>\n
List insert() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1) Inserting an element to the List<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", \"is\", \"btechgeeks\"]\r\n# Give the position as static input and store it in another variable.\r\ngvn_positin = 1\r\n# Give the element as static input and store it in another variable.\r\ngvn_elemnt = \"this\"\r\n# Pass the given position and given element as arguments to the insert()\r\n# function for the given list to insert the given element at the given position.\r\ngvn_lst.insert(gvn_positin, gvn_elemnt)\r\n# Print the given list after inserting the given element.\r\nprint(\"The given list after inserting the given element: \")\r\nprint(gvn_lst)\r\n<\/pre>\n
The given list after inserting the given element: \r\n['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
2)Inserting tuple as an element to the List<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"good\", \"morning\", \"btechgeeks\"]\r\n# Give the position as static input and store it in another variable.\r\ngvn_positin = 1\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (1, 3, 5, 7)\r\n# Pass the given position and given tuple as arguments to the insert()\r\n# function for the given list to insert the given tuple at the given position.\r\ngvn_lst.insert(gvn_positin, gvn_tupl)\r\n# Print the given list after inserting the given Tuple.\r\nprint(\"The given list after inserting the given Tuple: \")\r\nprint(gvn_lst)\r\n<\/pre>\n
The given list after inserting the given Tuple: \r\n['good', (1, 3, 5, 7), 'morning', 'btechgeeks']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_positin = int(input(\"Enter some random number = \"))\r\n# Give the element as user input using the input() function and \r\n# store it in another variable.\r\ngvn_elemnt = input(\"Enter some random string = \")\r\n# Pass the given position and given element as arguments to the insert()\r\n# function for the given list to insert the given element at the given position.\r\ngvn_lst.insert(gvn_positin, gvn_elemnt)\r\n# Print the given list after inserting the given element.\r\nprint(\"The given list after inserting the given element: \")\r\nprint(gvn_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40 70\r\nEnter some random number = 2\r\nEnter some random string = hello\r\nThe given list after inserting the given element: \r\n[10, 20, 'hello', 30, 40, 70]<\/pre>\n
\n