Aug-30-2019, 03:45 PM
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:
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']
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']