{"id":24702,"date":"2021-10-25T09:11:43","date_gmt":"2021-10-25T03:41:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=24702"},"modified":"2021-11-05T17:20:41","modified_gmt":"2021-11-05T11:50:41","slug":"python-list-copy-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-copy-method-with-examples\/","title":{"rendered":"Python List copy() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List clear() 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 copy() Method in Python:<\/strong><\/p>\n The copy() method returns a duplicate (copy) of the specified list.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values: <\/strong>This method has no parameters.<\/p>\n Return Value:<\/strong><\/p>\n A new list is returned by the copy() function. It makes no changes to the original 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 To copy a list, we can also use the = operator.<\/p>\n However, there is one drawback to copying lists in this manner. When you change a new list, you also change the old list. It’s because the new list refers to or points to the same old list object.<\/p>\n Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n For Example:<\/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 clear() 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.copy()<\/pre>\n
Given List = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
The given list is : ['hello', 'this', 'is', 'btechgeeks']\r\nThe new list after applying copy() function is :\r\n['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
Given List = [20, 40, 60, 80, 100]<\/pre>\n
The given list is : [20, 40, 60, 80, 100]\r\nThe new list after applying copy() function is :\r\n[20, 40, 60, 80, 100]<\/pre>\n
List copy() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", \"this\", \"is\", \"btechgeeks\"]\r\n# Print the given list.\r\nprint(\"The given list is : \", gvn_lst)\r\n# Copy all the elements of the given list into the new list using the copy()\r\n# function and store it in another variable.\r\nnew_lst = gvn_lst.copy()\r\n# Print the above new list which is the duplicate of the given list.\r\nprint(\"The new list after applying copy() function is :\")\r\nprint(new_lst)\r\n<\/pre>\n
The given list is : ['hello', 'this', 'is', 'btechgeeks']\r\nThe new list after applying copy() function is :\r\n['hello', 'this', 'is', '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# Print the given list.\r\nprint(\"The given list is : \", gvn_lst)\r\n# Copy all the elements of the given list into the new list using the copy()\r\n# function and store it in another variable.\r\nnew_lst = gvn_lst.copy()\r\n# Print the above new list which is the duplicate of the given list.\r\nprint(\"The new list after applying copy() function is :\")\r\nprint(new_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 20 40 60 80 100\r\nThe given list is : [20, 40, 60, 80, 100]\r\nThe new list after applying copy() function is :\r\n[20, 40, 60, 80, 100]<\/pre>\n
copying List using ‘=’ Operator<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [20, 40, 60, 80, 100]\r\n# copy the elements of the given list into new list using the '=' operator\r\n# store it in another variable.\r\nnew_lst = gvn_lst\r\n# Add an element to the new list using the append() function\r\nnew_lst.append('python')\r\n# Print the given original list.\r\nprint(\"The given original list is:\", gvn_lst)\r\n# Print the new list obtained.\r\nprint(\"The new list obtained is :\", new_lst)\r\n<\/pre>\n
The given original list is: [20, 40, 60, 80, 100, 'python']\r\nThe new list obtained is : [20, 40, 60, 80, 100, 'python']<\/pre>\n
copying List using Slicing<\/h3>\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks', 123]\r\n# copy the elements of the given list into new list using the slicing.\r\n# store it in another variable.\r\nnew_lst = gvn_lst[:]\r\n# Add an element to the new list using the append() function\r\nnew_lst.append(1000)\r\n# Print the given original list.\r\nprint(\"The given original list is:\", gvn_lst)\r\n# Print the new list obtained.\r\nprint(\"The new list obtained is :\", new_lst)\r\n<\/pre>\n
The given original list is: ['good', 'morning', 'btechgeeks', 123]\r\nThe new list obtained is : ['good', 'morning', 'btechgeeks', 123, 1000]<\/pre>\n
\n