Python | Cloning or copying a list

Python Methods and Functions

In this article, we'll look at different ways to copy or clone a list in Python. These different copying methods take different execution times, so we can compare them in time.

  1. Using slicing technique
    This is the easiest and fastest way to clone a list. This method is considered when we want to modify the list and also keep a copy of the original. In this we make a copy of the list itself along with the link. This process is also called cloning. This method takes about 0.039 seconds and is the fastest method.

    # Python program to copy or clone a list
    # Using the slice operator

    def Cloning (li1):

    li_copy = li1 [:]

    return li_copy

     
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1)

    print ( "After Cloning:" , li2)

    Output:
    Original list: [4, 8, 2, 10, 15, 18]
    After cloning: [4, 8, 2, 10, 15, 18]

  2. Using the extend () method
    Lists can be copied to a new list using the exte () function. This adds each element of the iterable object (like another list) to the end of the new list. It will take about 0.053 seconds. 
    Example:

    # Python code to clone or copy a list
    # Using the built-in extend ( )

    def Cloning (li1):

    li_copy = []

    li_copy.extend (li1)

    return li_copy

     
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1)

    print ( "After Cloning:" , li2)

    Output:

     Original List: [4, 8, 2, 10, 15 , 18] After Cloning: [4, 8, 2, 10, 15, 18] 
  3. Using the list () method
    This is the easiest way cloning a list using the built-in list () function. It will take about 0.075 seconds. 
    Example:

    # Python code to clone or copy a list
    # Using the built-in list of functions ( )

    def Cloning (li1):

    li_copy = list (li1)

    return li_copy

     
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1)

    print ( "After Cloning:" , li2)

    Output:

     Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18] 
  4. Using the Shallow Copy method
    This copying method using copy.copy is well explained in the article Shallow Copy . This will take about 0.186 seconds.
  5. Using a list comprehension
    The list comprehension method can be used to copy all items individually from one list to another. It takes about 0.217 seconds.

    # Python code to clone or copy a list
    # Using a comprehension list

    def Cloning (li1):

    li_copy = [i for i in li1]

    return li_copy

     
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1)

    print ( "After Cloning:" , li2)

    Output:

     Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18] 
  6. Using the append () method
    This can be used to add and add items to a list or copy them to a new th list. Used to add items to the last position of the list. This takes about 0.325 seconds and is the slowest cloning method.

    # Python code to clone or copy a list
    # Using append ()

    def Cloning (li1):

    li_copy = []

    for item in li1: li_copy.append (item)

      return li_copy

      
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1)

    print ( "After Cloning:" , li2)

    Output:

     Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18] 
  7. Usage copy () method
    The built-in copy method is used to copy all items from one list to another. It will take about 1.488 seconds. 
    Example:

    # Python code to clone or copy a list
    # Using the built-in copy ( )

    def Cloning (li1):

    li_copy = []

    li_copy = li1.copy ()

    return li_copy

     
    Driver code

    li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]

    li2 = Cloning (li1)

    print ( "Original List:" , li1 )

    print ( "After Cloning: " , li2)

    Output:

     Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18] 
  8. Using Deep Copy
    This copying method is well explained in the article Deep Copy . This takes about 10.59 seconds and is the slowest cloning method.

Refers to stack overflow .