Python Forum
List modification returns none
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List modification returns none
#1
Here's the end of a short program I wrote:

targetm = []
def chop(lst):
    tgti = len(lst) - 1 #this is the last item index in the list
    del lst[tgti] #delete the last item
    del lst[0] #delete the first item
def middle(targetm):
    targetm = target[1:len(target)-1]
choplist = chop(targetc) #targetc is a copy of list "target", which was created earlier by user input
middlelist = middle(targetm)
print('New list generated by chop function is',choplist)
print('New list generated by middle function is',middlelist)
Chop is supposed to delete the first and last items in a list and return None.

Middle is supposed to return the original list without the first and last items.

Program output is as follows:

Enter value for list. If done, enter "done": a
Enter value for list. If done, enter "done": e
Enter value for list. If done, enter "done": i
Enter value for list. If done, enter "done": o
Enter value for list. If done, enter "done": u
Enter value for list. If done, enter "done": done
Created list is ['a', 'e', 'i', 'o', 'u'] .
The list has 5 members.
New list generated by chop function is None
New list generated by middle function is None

I have two questions:

1. Why _does_ chop return None?
2. Why _doesn't_ middle simply remove first and last items of the list?

Thanks!
Mark
Reply
#2
Your function are not using return to return something useful, by default a function will return None.
Reply
#3
Great!

One further question. I understand lists are mutable, but if I copy the list and work on the copy then why is the original list affected? Check out this snippet:

targetc = target #make copy of original list
print('Copy of original list is', targetc)
def chop(lst):
    tgti = len(lst) - 1 #this is the last item index in the list
    del lst[tgti] #delete the last item
    del lst[0] #delete the first item
    return(lst)
print('New list generated by chop function is',chop(targetc)) #performing function on copied list
print('Original list is now',target)
quit()
The output is:

Enter value for list. If done, enter "done": a
Enter value for list. If done, enter "done": e
Enter value for list. If done, enter "done": i
Enter value for list. If done, enter "done": o
Enter value for list. If done, enter "done": u
Enter value for list. If done, enter "done": done
Created list is ['a', 'e', 'i', 'o', 'u'] .
The list has 5 members.
Copy of original list is ['a', 'e', 'i', 'o', 'u']
New list generated by chop function is ['e', 'i', 'o']
Original list is now ['e', 'i', 'o']
Reply
#4
Line 1 does not make a copy of the list. It just creates another variable pointing to the original list.
In another thread one of the better programmers here posted a link that really helps to explain this (I think yesterday). Watch the video, it will become clear.
How variables work in Python
Reply
#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
#6
That makes sense... thanks for the help!
Reply
#7
Maybe I don't fully get it yet. In Charles Severence's _Python for Everybody_ book, he includes this example:

https://www.screencast.com/t/txn9O0ozu

Going back to my initial example where I failed to use RETURN, why, here, is RETURN the difference between changing the list or not doing so?

I almost want to think a defined function without a return line won't ever do anything, but I'm guessing an experienced programmer could give a list of examples where this is false.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function returns dataframe as list harum 2 1,410 Aug-13-2022, 08:27 PM
Last Post: rob101
  function that returns a list of dictionaries nostradamus64 2 1,762 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Scaled scatter modification yvrob 1 1,976 Nov-08-2019, 04:05 AM
Last Post: yvrob
  Python script modification rajannn 1 1,919 Oct-06-2019, 07:55 PM
Last Post: micseydel
  Inspect.getmembers with isclass returns an empty list Aldar 1 2,787 Oct-02-2019, 01:48 PM
Last Post: Aldar
  returns index of list if contains a word zarize 0 1,831 Sep-09-2019, 09:29 AM
Last Post: zarize
  Code modification okrus 0 1,810 Jun-24-2019, 01:23 PM
Last Post: okrus
  pyPDF2 nautilus columns modification AJBek 1 2,912 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,582 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