Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 03-lists.md #715

Open
wants to merge 1 commit into
base: gh-pages
from

Conversation

@ConradGGit
Copy link

commented Sep 13, 2019

Changed instructions on copying lists. Instead of using

my_salsa = list(salsa)

changed to

my_salsa = copy.deepcopy(salsa)

This is because using list() is less robust. The goal was to generate a new list which could be modified, without changing the original list.

However, list() will not achieve this if the original list contains a nested list. Whereas deepcopy() will work for any number of nested lists.

The following code illustrates this difference.

TestList_1 = [1,"apple", ["NestedListItem1","NestedListItem2"]]
print('The original list:', TestList_1)
print('Now we use the list() function to create a new list, and change a non-nested element of the list.')
NewList_1 = list(TestList_1)
NewList_1[0] = 2
print('The new list:',NewList_1)
print('In this case the original list remains unchanged.')
print('The original list:',TestList_1)
print('However, now we change a nested element of the new list.')
NewList_1[2][0]= "ChangedNestedListItem1"
print('The new list:',NewList_1)
print('In this case the old list gets changed.')
print('The old list:',TestList_1)

import copy
print('This time do the same thing, but use copy.deepcopy() instead of list().')
TestList_1 = [1,"apple", ["NestedListItem1","NestedListItem2"]]
print('The original list:', TestList_1)
print('Now we use the copy.deepcopy() function to create a new list, and change a non-nested element of the list.')
NewList_1 = copy.deepcopy(TestList_1)
NewList_1[0] = 2
print('The new list:',NewList_1)
print('In this case the original list remains unchanged.')
print('The original list:',TestList_1)
print('However, now we change a nested element of the new list.')
NewList_1[2][0]= "ChangedNestedListItem1"
print('The new list:',NewList_1)
print('In this case the old list also remains unchanged.')
print('The old list:',TestList_1)
Changed instructions on copying lists.  Instead of using my_salsa = list(salsa), changed to my_salsa = copy.deepcopy(salsa).

This is because using list() is less robust.  The goal was to generate a new list which could be modified, without changing the original list.

However, list() will not achieve this if the original list contains a nested list.  Whereas deepcopy() will work for any number of nested lists.

The following code illustrates this difference.

TestList_1 = [1,"apple", ["NestedListItem1","NestedListItem2"]]
print('The original list:', TestList_1)
print('Now we use the list() function to create a new list, and change a non-nested element of the list.')
NewList_1 = list(TestList_1)
NewList_1[0] = 2
print('The new list:',NewList_1)
print('In this case the original list remains unchanged.')
print('The original list:',TestList_1)
print('However, now we change a nested element of the new list.')
NewList_1[2][0]= "ChangedNestedListItem1"
print('The new list:',NewList_1)
print('In this case the old list gets changed.')
print('The old list:',TestList_1)

import copy
print('This time do the same thing, but use copy.deepcopy() instead of list().')
TestList_1 = [1,"apple", ["NestedListItem1","NestedListItem2"]]
print('The original list:', TestList_1)
print('Now we use the copy.deepcopy() function to create a new list, and change a non-nested element of the list.')
NewList_1 = copy.deepcopy(TestList_1)
NewList_1[0] = 2
print('The new list:',NewList_1)
print('In this case the original list remains unchanged.')
print('The original list:',TestList_1)
print('However, now we change a nested element of the new list.')
NewList_1[2][0]= "ChangedNestedListItem1"
print('The new list:',NewList_1)
print('In this case the old list also remains unchanged.')
print('The old list:',TestList_1)
@maxim-belkin

This comment has been minimized.

Copy link
Contributor

commented Sep 18, 2019

Thank you for the PR, @ConradGGit.

Yes, copy.deepcopy() is definitely more "robust" than list() when it comes to copying lists. However, it is one step further than what we want to talk about in this introduction to Python. We can add a note to the instructor guide (to help instructors prepare answer such questions), but I don't think we have the time in the main lesson to talk about copy.deepcopy.

@annefou, what do you think?

@annefou

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2019

your suggestion @ConradGGit is great but too complex for an introduction to python. So I agree with you @maxim-belkin. Adding some information in the instructor notes is a very good idea.

@ConradGGit

This comment has been minimized.

Copy link
Author

commented Sep 20, 2019

Thanks for your feedback Anne and Maxim I really appreciate it and I am more than happy to put this in the instructor notes as you suggest.

Just so I understand though, would you mind letting me know why copy.deepcopy() is considered complicated?

As far as I can tell, the only additional complication compared to list() is that we would need to do import copy. But we have already covered importing libraries several times by this point in the course so I would have thought all students would be comfortable with this. I guess it raises the issue of nested lists, but again this doesn't seem that hard to explain.

As I say, I am happy to go along with the consensus and put this in the notes, but I just thought I would ask the question so I can understand for future reference :)

@maxim-belkin

This comment has been minimized.

Copy link
Contributor

commented Sep 20, 2019

It's not that copy.deepcopy is complicated, it's the sheer amount of information that learners have to remember. At the moment, we can't (responsibly) add one more layer. You can argue that list() is also "a layer of information" and... I would have difficult time arguing with it because it certainly is...

I'm marking this PR as "discussion" to get opinions from the community.

@ldko

This comment has been minimized.

Copy link
Contributor

commented Sep 23, 2019

I see why it might be desirable to not teach copy.deepcopy with the quantity of material being taught already. However, it seems to me that many of the learners won't remember how they need to go about copying a list (whether by list() or copy.deepcopy) or doing most things they are seeing only once in a two-day workshop and would need to look back at the lesson or elsewhere for the function/syntax in the future anyway. If it were me learning, I would probably remember that there was something weird that I need to do to copy a list, but not necessarily how to do it. In such a case, I would find it most helpful to have included in the material the best practice/way that always works for copying the list. Also, I don't think it is a bad thing to remind learners about how they frequently may want to import functionality from various modules to do one thing or another.

Since the line 'my_salsa = list(salsa)' is in between a discussion of mutability and nested lists. To me telling the the learner how to make a copy of a list then immediately teach them a list it won't work on is like giving the learner a flashlight with no batteries and sending them into a dark room.

@maxim-belkin

This comment has been minimized.

Copy link
Contributor

commented Sep 23, 2019

Thanks, Lauren.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.