{"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>
\nList in Python:<\/strong><\/p>\n

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

list.insert(position, element)<\/pre>\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

Given List = [\"hello\", \"is\", \"btechgeeks\"]\r\nGiven position = 1\r\nGiven element = \"this\"<\/pre>\n

Output:<\/strong><\/p>\n

The given list after inserting the given element: \r\n['hello', 'this', 'is', 'btechgeeks']<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = [\"good\", \"morning\", \"btechgeeks\"]\r\nGiven position = 1\r\nGiven Tuple = (1, 3, 5, 7)<\/pre>\n

Output:<\/strong><\/p>\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