Как скопировать массив в другой массив python
Перейти к содержимому

Как скопировать массив в другой массив python

  • автор:

copy — Shallow and deep copy operations¶

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations (explained below).

Return a shallow copy of x.

Return a deep copy of x.

exception copy. Error ¶

Raised for module specific errors.

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.

Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.

The deepcopy() function avoids these problems by:

keeping a memo dictionary of objects already copied during the current copying pass; and

letting user-defined classes override the copying operation or the set of components copied.

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

Shallow copies of dictionaries can be made using dict.copy() , and of lists by assigning a slice of the entire list, for example, copied_list = original_list[:] .

Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.

In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__() . The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the __deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument. The memo dictionary should be treated as an opaque object.

Discussion of the special methods used to support object state retrieval and restoration.

Array Copying in Python

Let us see how to copy arrays in Python. There are 3 ways to copy arrays :

  • Simply using the assignment operator.
  • Shallow Copy
  • Deep Copy

Assigning the Array

We can create a copy of an array by using the assignment operator (=).

Syntax :

In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use = operator user thinks that this creates a new object; well, it doesn’t. It only creates a new variable that shares the reference of the original object.

Example:

Python3

Output :

We can see that both the arrays reference the same object.

Shallow Copy

A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. The copying process does not recurse and therefore won’t create copies of the child objects themselves. In the case of shallow copy, a reference of the object is copied in another object. It means that any changes made to a copy of the object do reflect in the original object. We will be implementing shallow copy using the view() function.

Example :

Python3

This time although the 2 arrays reference different objects, still on changing the value of one, the value of another also changes.

Deep Copy

Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In the case of deep copy, a copy of the object is copied into another object. It means that any changes made to a copy of the object do not reflect in the original object. We will be implementing deep copy using the copy() function.

How to Copy a List in Python (5 Techniques w/ Examples)

Lists are commonly used data structures in Python. We will often encounter situations wherein we need to make a copy of a list, and you may ask yourself, "How can I copy a list in Python?" or "Which copy method suits my requirements best?"

This tutorial will teach you how to copy or clone a list using several different techniques:

  • The assignment operator
  • The slicing syntax
  • The list.copy() method
  • The copy.copy() function
  • The copy.deepcopy() function

We will also discuss their usage and technical aspects in detail.

Copy a List Using the Assignment Operator

Suppose you use the assignment operator (=) to copy a list by assigning an existing list variable to a new list variable. In this case, you’re not actually creating a copy of the list; you’re just creating an alias that points to the exact same location in memory where the original list object exists. Let’s expand on the details and look closer.

Suppose we have the list variable, org_list , defined as follows:

Then, we assign it to a new variable, cpy_list , hoping to make a copy of it for future use:

However, you need to know the variable cpy_list isn’t a true copy of the original list. You may ask, "Why isn’t it a true copy of the original list?" This is a great question because, as you’ll see below, printing these two variables returns the exact the same values.

As expected, the lists contain the same values. But, let’s see what happens if we modify the original list.

Any modification to the original list will change the copied list, too.

The following illustration shows what’s happening once the source code is executed.

In fact, when you assign one variable to another, both variables are referencing the same object in memory, not two separate ones. This means both variables point to the same object via their references. When more than one variable references the same object, it’s called a shared reference or object.

Any modification to a shared mutable object via one of the variables that points to it affects the other variable that references the same object.

So, using the assignment operator doesn’t make a true copy of a list; it just creates an alias for the same object in memory.

But what if we want to make an independent copy of a list? In the following section, we’ll learn how to make shallow copies of a list.

The Shallow Copy Techniques

We just learned that assignments always store references to objects and don’t make an actual copy of those objects. However, it’s essential to know that changing a mutable object affects other objects that use the same reference in our code. So, we need to let Python know explicitly to copy an object if we want more than just a copy of the reference to that object. Generally speaking, there are two ways of making an independent copy of a list: a shallow copy and a deep copy. This section will discuss shallow copy and the different ways of implementing it.

Simply put, making a shallow copy of a compound list creates a new compound list and uses the references to the objects that the original list used.

NOTE: A compound object is an object that contains other objects, e.g., lists or dictionaries.

Shallow Copy Using List Slicing

To understand the shallow copy concept, let’s begin with an example. Assume we have a compound list as follows:

Then, we can create a shallow copy of it using list slicing syntax:

If we run the following print statements, we can see, both return exactly the same values.

Now, let’s append a new item to the original list and run the print statements again:

The modification doesn’t affect the copied list. But, this isn’t the whole story. Let’s try another scenario and change one of the items in the nested list to see what happens:

Although making a shallow copy of a list produces a true copy of the original list, any modifications to the nested elements within it will be reflected in both lists. The reason is that the nested list in the copied list uses the same shared reference as the one in the original list. In other words, the nested lists in the copied list are tied to the nested lists in the original list. This is why we call it a shallow copy — because only a new top-level object is created while anything deeper uses a shared reference with the original list.

Now, let’s look at some other ways of making shallow copies of a list.

The Python list.copy() Method

Earlier, we discussed creating a shallow copy via the slicing syntax. In this section, we’ll learn about a built-in method that Python programmers commonly use for copying lists. The Python copy() method returns a shallow copy of the list without taking any parameters. Let’s try it out:

Although org_list and cpy_list have the same values, as the output from the id() function shows, they end up in different locations in memory. However, exposing the memory addresses of the inner lists in both the original and copied lists reveals that they refer to the same location in memory, meaning we made a shallow copy of the original list.

The Python copy.copy() Function

The other useful way of making a shallow copy of a list is the copy.copy() function. To use it, we import the copy module and then pass the list we want to copy to the copy.copy() function. Let’s try it out:

Now, let’s append a new item to the original list, print both lists again, and check the output; nevertheless, we can predict the output before running the code below.

The copy.copy() method has made a true copy of the original list. However, it’s still a shallow copy, and the nested lists refer to exactly the same memory location. In other words, the copy.copy() function only makes top-level copies and doesn’t copy nested objects. So, any modifications in either the original or copied list’s nested objects reflects in the other list’s nested objects.

What if we want a fully independent copy of a deeply nested list? In the next section, we’ll discuss how to perform a deep copy in Python.

The Python copy.deepcopy() Function

The copy.deepcopy() function recursively traverses a list to make copies of each of its nested objects. In other words, it makes a top-level copy of a list and then recursively adds copies of the nested objects from the original list into the new copy. This produces a fully independent copy from the original list, and any changes made to the nested objects of either will not be reflected in the other.

Like the copy.copy() function, the copy.deepcopy() function belongs to the copy module. Let’s try it out:

The output of the code above clearly shows that copy.deepcopy() has made a true copy of the original list, and even if we modify the inner list of the original list, it won’t be reflected in the deep copied list.

The code above shows that when we create a deep copy of a list, it also makes true copies of the nested objects. As mentioned earlier, the recursive deep copy produces a truly independent copy of the original list, which is why the inner lists in the original and copied lists point to two different memory locations. Obviously, any changes made to the inner list of one won’t be reflected in the other.

Conclusion

This tutorial discussed several different ways for copying a list in Python, such as the assignment operator, list slicing syntax, list.copy() , copy.copy() , and copy.deepcopy functions. Also, we discussed shallow and deep copies. I hope this tutorial helps you to better understand the different ways of copying lists in Python because they are critical to becoming a Pythonista.

About the author

Mehdi Lotfinejad

Mehdi is a Senior Data Engineer and Team Lead at ADA. He is a professional trainer who loves writing data analytics tutorials.

How to copy a list in Python

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

Lists are a commonly used data structure in Python. When you use lists, there may be a situation where you need to copy or clone a list. There are several methods we can use to copy or clone a list.

1. Use copy()

If you need a shallow copy of a list, the built-in copy() function can be used.

This function takes no parameters and returns a shallow copy of the list.

2. Use slicing

List slicing can be used to easily make a copy of a list. This method is called cloning. The original list will remain unchanged.

In this method, slicing is used to copy each element of the original list into the new list.

3. Use a for loop and append()

In this method, a for loop traverses through the elements of the original list and adds them one by one to the copy of the list, through append() . The built-in append() function takes a value as an argument and adds it to the end of a list.

This creates a clone of the original list, so the original list is not changed.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *