{"id":24757,"date":"2021-11-02T09:45:58","date_gmt":"2021-11-02T04:15:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=24757"},"modified":"2021-11-05T17:23:27","modified_gmt":"2021-11-05T11:53:27","slug":"python-list-extend-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-extend-method-with-examples\/","title":{"rendered":"Python List extend() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List count() 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 extend() Method in Python:<\/strong><\/p>\n The extend() method appends all iterable (list, tuple, string, etc.) items to the end of the list.<\/p>\n Syntax:<\/strong><\/p>\n All iterable elements are appended to the end of gvnlist in this case.<\/p>\n Parameter Values:\u00a0<\/strong><\/p>\n As previously stated, the extend() method accepts an iterable such as a list, tuple, string, and so on.<\/p>\n Return Value:<\/strong><\/p>\n The extend() method makes changes to the original list. It doesn’t return anything.<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Covered all examples on python concepts and missed checking out the Python List Method Examples<\/a> then go with the available link & gain more knowledge about List method concepts in python.<\/p>\n In the previous article, we have discussed Python List count() 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>\ngvnlist.extend(iterable)<\/pre>\n
Given Main list = ['hello', 'this']\r\nGiven Second List = ['python', 'programs']<\/pre>\n
The Modified list after extending is : ['hello', 'this', 'python', 'programs']<\/pre>\n
Given Main list = ['hello', 'this']\r\nGiven Second Tuple= ('python', 'programs', 'python')<\/pre>\n
The Modified list after extending is : ['hello', 'this', 'python', 'programs', 'python']<\/pre>\n
List extend() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
Example-1: Adding Another List<\/h5>\n
\n
# Give the main list as static input and store it in a variable.\r\nmainlst = ['hello', 'this']\r\n# Give the list which we want to extend as static input and store it in another variable.\r\nscndlist = ['python', 'programs']\r\n# Extend the second list to the main list using the extend()\r\n# function(Here it appends the second list to main list)\r\n\r\nmainlst.extend(scndlist)\r\n# Print the mainlist\r\nprint('The Modified list after extending is : ', mainlst)\r\n<\/pre>\n
The Modified list after extending is : ['hello', 'this', 'python', 'programs']<\/pre>\n
Example-2: Adding Another Set<\/h5>\n
\n
# Give the main list as static input and store it in a variable.\r\nmainlst = ['hello', 'this']\r\n# Give the set which we want to extend as static input and store it in another variable.\r\nscndset = {'python', 'programs', 'python'}\r\n# Extend the second set to the main list using the extend()\r\n# function(Here it appends the second set to main list)\r\n\r\nmainlst.extend(scndset)\r\n# Print the mainlist\r\nprint('The Modified list after extending is : ', mainlst)\r\n<\/pre>\n
The Modified list after extending is : ['hello', 'this', 'programs', 'python']<\/pre>\n
Example-3: Adding Another Tuple<\/h5>\n
\n
# Give the main list as static input and store it in a variable.\r\nmainlst = ['hello', 'this']\r\n# Give the tuple which we want to extend as static input and store it in another variable.\r\nscndtup = ('python', 'programs', 'python')\r\n# Extend the second tuple to the main list using the extend() function\r\n# (Here it appends the second tuple to the main list).\r\n\r\nmainlst.extend(scndtup)\r\n# Print the mainlist\r\nprint('The Modified list after extending is : ', mainlst)\r\n<\/pre>\n
The Modified list after extending is : ['hello', 'this', 'python', 'programs', 'python']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the main list as user input using list(),int(),split() and map() functions and store it in a variable.\r\nmainlst = list(\r\n map(int, input('Enter some random elements of the main list = ').split()))\r\n# Give the list which we want to extend as user input using list(),int(),split() and map() functions\r\n# and store it in another variable.\r\nscndlst = list(\r\n map(int, input('Enter some random elements of the second list = ').split()))\r\n# Extend the second list to the main list using the extend() function\r\n# (Here it appends the second list to the main list).\r\n\r\nmainlst.extend(scndlst)\r\n# Print the mainlist\r\nprint('The Modified list after extending is : ', mainlst)\r\n<\/pre>\n
Enter some random elements of the main list = 0 3 0 8\r\nEnter some random elements of the second list = 2 8 3 7 9 4\r\nThe Modified list after extending is : [0, 3, 0, 8, 2, 8, 3, 7, 9, 4]<\/pre>\n
\n