C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python List copy() MethodPython copy() method copies the list and returns the copied list. It does not take any parameter and returns a list. The method signature and examples are given below. Signaturecopy() ParametersNo parameter ReturnIt returns a copy of the list. Let's see some examples of copy() method to understand it's functionality. Python List copy() Method Example 1A Simple example which copy a list ot another and makes an new list. See the example below. # Python list copy() Method # Creating a list evenlist = [6,8,2,4] # int list copylist = [] # Calling Method copylist = evenlist.copy() # Displaying result print("Original list:",evenlist) print("Copy list:",copylist) Output: Original list: [6, 8, 2, 4] Copy list: [6, 8, 2, 4] Python List copy() Method Example 2Here, we are using list slicing concept to create a copy of the list. See the example below. # Python list copy() Method # Creating a list evenlist = [6,8,2,4] # int list copylist = [] # Calling Method copylist = evenlist[:] # Copy all the elements # Displaying result print("Original list:",evenlist) print("Copy list:",copylist) Output: Original list: [6, 8, 2, 4] Copy list: [6, 8, 2, 4] Python List copy() Method Example 3The assignment operator can also be used to copy a list into another. This is most general approach but not recommended. # Python list copy() Method # Creating a list evenlist = [6,8,2,4] # int list copylist = [] # Calling Method copylist = evenlist # Copy all the elements # Displaying result print("Original list:",evenlist) print("Copy list:",copylist) Output: Original list: [6, 8, 2, 4] Copy list: [6, 8, 2, 4]
Next TopicPython Lists
|