{"id":24694,"date":"2021-10-25T09:07:07","date_gmt":"2021-10-25T03:37:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=24694"},"modified":"2021-11-05T17:19:58","modified_gmt":"2021-11-05T11:49:58","slug":"python-list-append-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-append-method-with-examples\/","title":{"rendered":"Python List append() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String islower() 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 append() Method in Python:<\/strong><\/p>\n The append() method adds an element or item to the end of the list.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n The method only accepts one argument.<\/p>\n item<\/strong> – an item (number, string, list, etc.) that will be appended to the end of the list<\/p>\n Return Value:<\/strong><\/p>\n The append() method produces no output (returns None).<\/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
\nList in Python:<\/strong><\/p>\nlist.append(item)<\/pre>\n
Given list = [\"hello\", \"this\", \"is\"]\r\nItem to be added = \"btechgeeks\"<\/pre>\n
The given list after appending { btechgeeks } = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
Given list = [10, 15, 20, 25]\r\nItem to be added = 30<\/pre>\n
The given list after appending { 30 } = [10, 15, 20, 25, 30]<\/pre>\n
List append() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Appending an item to List<\/strong><\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", \"this\", \"is\"]\r\n# Give the string to be added as static input and store it in another variable.\r\nnew_item = \"btechgeeks\"\r\n# Append the above-given string item to the given list using the append()\r\n# function.\r\ngvn_lst.append(new_item)\r\n# Print the given list after appending(adding) the given string item.\r\nprint(\"The given list after appending {\", new_item, \"} = \", gvn_lst)\r\n<\/pre>\n
The given list after appending { btechgeeks } = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
2)Appending List to a List<\/strong><\/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 new list to be added as static input and store it in another variable.\r\nnew_lst = [1, 2, 3, 4, 5]\r\n# Append the above-given new list to the given list using the append()\r\n# function.\r\ngvn_lst.append(new_lst)\r\n# Print the given list after appending the given new list.\r\nprint(\"The given list after appending new list \", new_lst, \":\")\r\nprint(gvn_lst)\r\n<\/pre>\n
The given list after appending new list [1, 2, 3, 4, 5] :\r\n['good', 'morning', 'btechgeeks', [1, 2, 3, 4, 5]]<\/pre>\n