Nerd For Tech
Published in

Nerd For Tech

For Noobs: How to Copy a List in Python 3.

This article is by a noob and for noobs. It’s also for anyone teaching noobs, of course.

Copying lists using the equals sign alone is, for a noob, a recipe for disaster. There’s a great danger that you’ll get a bug in your code that takes you a very long time to fix.

This is how to copy a list, at least at first, until some of the outrageous subtleties of how lists work are mastered.

Suppose that the list of the best coders of each year is always only slightly different, so instead of making a new list from scratch, you just copy the old list, renaming to the new year and then modify it accordingly. Suppose that James Senior gets a lot of help from James Junior so you put them together as if they were a sort of team. The result is a nested list. By 2020 James Junior has quit coding and his sister Patricia has taken over his role in helping dad. No other change to the list. So the only modification needed is to replace James Junior with Patricia. First you import the copy module and use the deepcopy function to copy the old list. Then you modify the list, replacing ‘James Junior’ with ‘Patricia’.

best_coders_of_2019 = [['James Junior', 'James Senior'], 'Mary', 'Pete']

import copy

best_coders_of_2020 = copy.deepcopy(best_coders_of_2019)

best_coders_of_2020[0][0] = 'Patricia'

That’s all there is to it. If you print out best_coders_of_2020 you’ll see:

[['Patricia', 'James Senior'], 'Mary', 'Pete']

and if you print out best_coders_of_2019 you’ll see:

[['James Junior', 'James Senior'], 'Mary', 'Pete']

Hunky dory, and pretty simple, right? If you just wanted to know how to copy a list safely a noob, you can stop reading here. The rest of this article is of academic interest only to you.

It probably seems strange that such a straightforward, intuitive process requires importing a module.

Why not use the equals sign alone? It would look like this:

#Noobs should not code like this.

best_coders_of_2019 = [['James Junior', 'James Senior'], 'Mary', 'Pete']

best_coders_of_2020 = best_coders_of_2019

best_coders_of_2020[0][0] = 'Patricia'

It’s very natural to think of that, since it works well with strings and numbers to use the equals sign alone. If you print out best_coders_of_2020 you’ll see:

[['Patricia', 'James Senior'], 'Mary', 'Pete']

and if you print out best_coders_of_2019 you’ll see:

[['Patricia', 'James Senior'], 'Mary', 'Pete']

See the problem? Copying the 2019 list using the equals sign alone and then modifying it also changed the 2019 list. Oh my goodness.

What noob would have expected that? Python 3 is designed to work this way. As a noob, I don’t quite understand the ins and outs of it (it’s really difficult to understand) but it’ something to do with copying lists in less time and using less memory to store lists. Noobs don’t need to know why. There are a bunch of ways to copy lists but only the complicated-looking deepcopy function does it in what seems like a straightforward way.

A word of warning: I think copying dictionaries using the equals sign alone has the same surprising (to the noob) behavior. The good news is that deepcopy works about the same way with dictionaries as it does with lists.

@bartshmatthew on Twitter if you don’t want comment here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Christopher Bartsh

Matthew Christopher Bartsh

I always follow back. I usually follow anyone who makes an interesting or okay response to one my articles. I often clap. I never give fewer than fifty claps.