{"id":3752,"date":"2023-10-24T16:21:01","date_gmt":"2023-10-24T10:51:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=3752"},"modified":"2023-11-10T11:57:13","modified_gmt":"2023-11-10T06:27:13","slug":"python-how-to-insert-an-element-at-specific-index-in-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-insert-an-element-at-specific-index-in-list\/","title":{"rendered":"Python : How to Insert an element at specific index in List ?"},"content":{"rendered":"
Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.<\/p>\n
An index refers to a position within an list.<\/p>\n
Given, a list the task is to insert an element at specific index in given list.<\/p>\n
Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n Suppose we want to insert ‘python’ in a givenlist at index 3 we can pass arguments like<\/p>\n insert(3,value)<\/strong> where value=‘python’<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We know that the starting index of list is 0 so we give 0 as index while inserting the given value.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We know that the last index of the list is len(list)-1 to insert element at end we use len(list) as parameter.<\/p>\n Passing this index to insert function inserts the element at the end of the list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n By slicing we can insert it easily .<\/p>\n Splice given list from 0 to index-1 and merge all of list2’s elements into it. Then, from index to the end of the list, merge all of the remaining elements.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. …<\/p>\ngivenlist.insert(index, element)<\/pre>\n
index - the position in the array at which the element must be inserted.\r\nelement - the element that will be added to the list.<\/pre>\n
This method does not return a value, but instead inserts the specified element at the specified index.<\/pre>\n
2)Inserting an element at a specific position<\/h3>\n
# given list\r\ngivenlist = ['hello', 'this', 'is', 'BTechGeeks']\r\n# given value\r\nvalue = 'python'\r\n# given index\r\nindex = 3\r\n# inserting the given value at given index\r\ngivenlist.insert(index, value)\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'this', 'is', 'python', 'BTechGeeks']<\/pre>\n
3)Inserting element at the first position of the list<\/h3>\n
# given list\r\ngivenlist = ['hello', 'this', 'is', 'BTechGeeks']\r\n# given value\r\nvalue = 'python'\r\n# inserting the given value at starting position\r\ngivenlist.insert(0, value)\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['python', 'hello', 'this', 'is', 'BTechGeeks']<\/pre>\n
4)Inserting element at the end position of the list<\/h3>\n
# given list\r\ngivenlist = ['hello', 'this', 'is', 'BTechGeeks']\r\n# given value\r\nvalue = 'python'\r\n# inserting the given value at end position\r\ngivenlist.insert(len(givenlist), value)\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'this', 'is', 'BTechGeeks', 'python']<\/pre>\n
5)Insert all elements of second list at specific index in given list<\/h3>\n
# given list\r\ngivenlist = ['hello', 'this', 'is', 'BTechGeeks']\r\n# given value\r\ngivenlist2 = ['welcome', 'to', 'python']\r\n# given index\r\nindex = 2\r\n# inserting the given value using slicing\r\ngivenlist = givenlist[:index]+givenlist2+givenlist[index:]\r\n# print the list\r\nprint(givenlist)\r\n<\/pre>\n
['hello', 'this', 'welcome', 'to', 'python', 'is', 'BTechGeeks']<\/pre>\n
\n