Python Forum
List modification returns none
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List modification returns none
#5
targetc = target #make copy of original list
does not make a unique copy of a list, it just has another variable with a pointer to the same list.
https://docs.python.org/3/tutorial/datas...e-on-lists Wrote:list.copy()
  • Return a shallow copy of the list. Equivalent to a[:].

targetc = target.copy()
or
targetc = target[:]
Also see
https://docs.python.org/3/library/copy.html Wrote: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).

Interface summary:

copy.copy(x)
  • Return a shallow copy of x.

copy.deepcopy(x[, memo])
  • 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, array, 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.
Reply


Messages In This Thread
List modification returns none - by Mark17 - Aug-30-2019, 02:59 PM
RE: List modification returns none - by Yoriz - Aug-30-2019, 03:15 PM
RE: List modification returns none - by Mark17 - Aug-30-2019, 03:45 PM
RE: List modification returns none - by jefsummers - Aug-30-2019, 04:09 PM
RE: List modification returns none - by Yoriz - Aug-30-2019, 04:10 PM
RE: List modification returns none - by Mark17 - Aug-30-2019, 07:16 PM
RE: List modification returns none - by Mark17 - Sep-02-2019, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 5 348 May-02-2024, 05:39 PM
Last Post: tester_V
  function returns dataframe as list harum 2 1,462 Aug-13-2022, 08:27 PM
Last Post: rob101
  function that returns a list of dictionaries nostradamus64 2 1,792 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Scaled scatter modification yvrob 1 1,997 Nov-08-2019, 04:05 AM
Last Post: yvrob
  Python script modification rajannn 1 1,950 Oct-06-2019, 07:55 PM
Last Post: micseydel
  Inspect.getmembers with isclass returns an empty list Aldar 1 2,818 Oct-02-2019, 01:48 PM
Last Post: Aldar
  returns index of list if contains a word zarize 0 1,858 Sep-09-2019, 09:29 AM
Last Post: zarize
  Code modification okrus 0 1,841 Jun-24-2019, 01:23 PM
Last Post: okrus
  pyPDF2 nautilus columns modification AJBek 1 2,943 Jun-07-2019, 04:17 PM
Last Post: micseydel
  Access the full list using an api that rather returns only a few elements bharath 1 2,599 Mar-08-2018, 02:55 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020