Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← The Hard Way Solution - Intro to Computer Science

Get Embed Code
2 Languages

Showing Revision 5 created 05/25/2016 by Udacity Robot.

  1. So the correct answer is the second one. So hopefully, no one picked
  2. the first answer. We usually do try to do things the easy way.
  3. There is sort of an exception to that in this whole unit. That
  4. we're learning ourselves how to define a hash table. Even though Python provides
  5. a dictionary type, which I'll talk about at the very end of the
  6. unit. That makes things much easier than what we're doing ourselves, but we
  7. really want to understand a lot of computer science by building our own hash
  8. table, and then we won't actually need to use it because there's a built-in
  9. text that works much better. But the answer is
  10. the second answer, and here's the reason why. So what
  11. happens when we evaluate this list multiplication, well, we have
  12. this empty list, let's draw it like this. And we
  13. create a new list, which is three copies of that
  14. list. But it's not copies, it's three references to it.
  15. So here's the new list. It has three elements. Each
  16. one of those elements refers to the same empty list.
  17. Then when we did table index 1 append, well, what
  18. happened is we follow this reference to whatever table index
  19. 1 is, this object we appended to it, added a
  20. new element. But because the references at position zero, one, and
  21. two, all refer to the same object, that changed all
  22. of those values, not just the value of table index 1.
  23. And we can see that if we print out the
  24. whole table, that the table contains three elements. But they're all
  25. the same. And they all refer to the same object.
  26. So any change we make to one changes all of them.