![]() |
|
[Tut] Python List copy() - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python List copy() (/thread-94150.html) |
[Tut] Python List copy() - xSicKxBot - 03-21-2020 Python List copy() Surprisingly, even advanced Python coders don’t know the details of the Code: copy()Definition and Usage: The Code: list.copy()Code: listHere’s a short example: Code: >>> lst = [1, 2, 3]
>>> lst.copy()
[1, 2, 3]In the first line, you create the list Code: lstPuzzle – Try It Yourself: https://repl.it/repls/PlaintiveSoulfulGeneric?lite=true Syntax: You can call this method on each list object in Python. Here’s the syntax: Code: list.copy()Arguments: The method doesn’t take any argument. Return value: The method Code: list.clear()Code: listVideo: Python List Copy Shallow Before you can truly understand the Code: copy()In object-oriented languages such as Python, everything is an object. The list is an object and the elements in the list are objects, too. A shallow copy of the list creates a new list object—the copy—but it doesn’t create new list elements but simply copies the references to these objects. ![]() You can see that the list below is only a shallow copy pointing to the same elements as the original list. In Python, the Code: list.copy()Here’s an example showing exact this scenario: Code: >>> lst = [6, 7, [1, 2], "hello"]
>>> lst_2 = lst.copy()
>>> lst_2[2].append(42)
>>> lst[2]
[1, 2, 42]Changing the third list element of the copied list impacts the third list element of the original list. Python List Copy Deep Having understood the concept of a shallow copy, it’s now easy to understand the concept of a deep copy. A shallow copy only copies the references of the list elements. A deep copy copies the list elements themselves which can lead to a highly recursive behavior because the list elements may be lists themselves that need to be copied deeply and so on. Here’s a simple deep copy of the same list as shown previously: ![]() In contrast to the shallow copy, the list Code: [1, 2]Note that in a deep copy, the string object must not be copied. Why? Because strings are immutable so you cannot change them (and, thus, there will be no dirty “side effects” seen by other copies of the list pointing to the same object in memory. To get a deep copy in Python, use the copy module and use the Code: deepcopy()Code: >>> import copy
>>> lst = [6, 7, [1, 2], "hello"]
>>> lst_2 = copy.deepcopy(lst)
>>> lst_2[2].append(42)
>>> lst[2]
[1, 2]Say, you want to copy the Code: listMethod | Description Code: list.copy()Returns a shallow copy of the Code: listCode: import copycopy.deepcopy(list)Import the Code: copyCode: listCode: list[:]Use slicing with default indices to create a shallow copy of the Code: listCode: list(x)use the built-in list constructor Code: list(...)Code: xCode: [el for el in lst]Use list comprehension to create a shallow copy of the original list Code: lstSlicing belongs to the fastest methods (very dirty benchmark here). If you need to refresh your Python slicing skills, here’s a tutorial on the Finxter blog: Related Articles: Python List Copy Not Working The main reason why the Code: list.copy()Code: copyCode: deepcopy(x)Code: xCode: >>> import copy
>>> lst = [[1, 2], 3, 4]
>>> lst_2 = copy.deepcopy(lst)How to copy a list and append an element in one line of Python code? Simply use slicing to copy the list and the list concatenation operator Code: +Code: [x]Code: xCode: lst
The third way to copy a list and append a new element is my personal favorite because it’s fast, easy-to-read, and concise. It uses the asterisk operator to unpack the elements of the original list into a new list. Python List Copy By Value Do you want to copy all elements in your list “by value”? In other words, you want not only the list object to be copied (shallow copy) but also the list elements (deep copy). This can be done with the Code: deepcopy()Code: copyCode: >>> import copy
>>> lst = [6, 7, [1, 2], "hello"]
>>> lst_2 = copy.deepcopy(lst)
>>> lst_2[2].append(42)
>>> lst[2]
[1, 2]The element 42 was not appended to the nested list of Code: lstPython List Copy With Slice You can simply copy a list Code: lstCode: lst[:]Code: lstRelated Articles: Python List Copy Without First Element To copy a list without its first element, simply use slicing Code: list[1:]Here’s an example: Code: >>> lst = [1, 2, 3, 4]
>>> lst[1:]
[2, 3, 4]To copy a list without its last element, simply use slicing Code: list[:-1]Here’s an example: Code: >>> lst = [1, 2, 3, 4]
>>> lst[:-1]
[1, 2, 3]The time complexity of shallow list copying—examples are Code: list.copy()Code: list[:]I wrote a quick script to evaluate that the time complexity of copying a list is, in fact, linear in the number of list elements: Code: import matplotlib.pyplot as plt
import time y = []
for i in [100000 * j for j in range(10)]: lst = list(range(i)) t0 = time.time() lst_2 = lst[:] t1 = time.time() y.append(t1-t0) plt.plot(y)
plt.xlabel("List elements (10**5)")
plt.ylabel("Time (sec)")
plt.show()Here’s the result: ![]() The runtime grows linearly in the number of list elements. Python List Copy Partially How to copy a list partially? To copy only the elements between Code: startCode: stopCode: list[start:stop]Python List Copy Multi-Dimensional List To copy a multi-dimensional list (a list of lists), you need to create a deep copy. You can accomplish this with the copy library’s deepcopy() method as follows: Code: >>> import copy
>>> lst = [[1, 2, 3], [4, 5, 6]]
>>> lst_2 = copy.deepcopy(lst)
>>> lst_2
[[1, 2, 3], [4, 5, 6]]Now check if the copy is really deep by clearing the first list element in the copy: Code: >>> lst_2[0].clear()
>>> lst
[[1, 2, 3], [4, 5, 6]]
>>> lst_2
[[], [4, 5, 6]]You can see that the copy was really deep because the first element of the Code: lstCode: lst_2Python List copy() Thread Safe Do you have a multiple threads that access your list at the same time? Then you need to be sure that the list operations (such as Code: copy()In other words: can you call the Code: copy()The answer is yes (if you use the cPythonimplementation). The reason is Python’s global interpreter lock that ensures that a thread that’s currently working on it’s code will first finish its current basic Python operation as defined by the cPython implementation. Only if it terminates with this operation will the next thread be able to access the computational resource. This is ensured with a sophisticated locking scheme by the cPython implementation. The only thing you need to know is that each basic operation in the cPython implementation is atomic. It’s executed wholly and at once before any other thread has the chance to run on the same virtual engine. Therefore, there are no race conditions. An example for such a race condition would be the following: the first thread reads a value from the list, the second threads overwrites the value, and the first thread overwrites the value again invalidating the second thread’s operation. All cPython operations are thread-safe. But if you combine those operations into higher-level functions, those are not generally thread safe as they consist of many (possibly interleaving) operations. Where to Go From Here? The Code: list.copy()Code: listCode: copy.deepcopy(list)Code: listYou’ve learned the ins and outs of this important Python list method. If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: Python One-Liners (Amazon Link). In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code! Get the book from Amazon! OFFICIAL BOOK DESCRIPTION: Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation. |