Return to Video

List Operations - Intro to Computer Science

  • 0:00 - 0:04
    [Narrator] I hope you're getting a sense that lists are very powerful.
  • 0:04 - 0:07
    That by using mutation, by having lists that could contain other lists
  • 0:07 - 0:10
    or any other kind of data we want,
  • 0:10 - 0:14
    we can build very complex data structures, and we can do lots of interesting things.
  • 0:14 - 0:16
    I'm going to introduce 1 more list operation,
  • 0:16 - 0:20
    which will allow us to add a new element at the end of the list.
  • 0:20 - 0:24
    We've seen that we can use lists to store complex data,
  • 0:24 - 0:27
    that elements of the list can be any type we want including other lists,
  • 0:27 - 0:30
    and we've seen that we can use mutation to change the value of a list,
  • 0:30 - 0:35
    and that mutation is visible through any reference to the same list object.
  • 0:35 - 0:39
    Now I'm going to introduce some other list operations.
  • 0:39 - 0:44
    The first one is append, and append is like a procedure but it's a method,
  • 0:44 - 0:47
    so we use it similar to the way we use to find on strings.
  • 0:47 - 0:52
    We'll have a list first, then a dot followed by append,
  • 0:52 - 0:55
    and what we pass in is the element we want to append to the list.
  • 0:55 - 0:59
    Append will add a new element to the end of a list,
  • 0:59 - 1:01
    and the important thing about append is that it's mutating
  • 1:01 - 1:04
    the list that its invoked on.
  • 1:04 - 1:08
    It's not creating a new list; it's mutating the old list.
  • 1:08 - 1:12
    As an example of the use of append let's assume that instead of
  • 1:12 - 1:16
    replacing curly in the 3 stooges, we want to end up with 4 stooges.
  • 1:16 - 1:19
    We'll add Shemp and add the other 3 as they are.
  • 1:19 - 1:24
    So what we want to do is to append Shemp at the end of the list we have.
  • 1:24 - 1:28
    We would do that by invoking append on the stooges,
  • 1:28 - 1:31
    passing in the string Shemp as the input.
  • 1:31 - 1:35
    Here's what happens after the first assignment,
  • 1:35 - 1:39
    the name stooges refers to the list containing the 3 elements,
  • 1:39 - 1:41
    Moe, Larry, and Curly.
  • 1:41 - 1:46
    When we invoke append it modifies that object,
  • 1:46 - 1:48
    adding a new element to it.
  • 1:48 - 1:54
    After the append, the list that stooges refers to now has 4 elements.
  • 1:54 - 1:56
    We have not created a new list.
  • 1:56 - 1:59
    Note that there's no assignment from the result of append.
  • 1:59 - 2:03
    What we've done is modify the value that stooges refers to
  • 2:03 - 2:06
    by adding a new element to it.
Title:
List Operations - Intro to Computer Science
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
CS101 - Intro to Computer Science
Duration:
02:06
Udacity Robot edited English subtitles for 11-21 List Operations
Udacity Robot edited English subtitles for 11-21 List Operations
Fran Ontanaya edited English subtitles for 11-21 List Operations
Udacity Robot edited English subtitles for 11-21 List Operations
Udacity Robot edited English subtitles for 11-21 List Operations
Amara Bot added a translation

English subtitles

Revisions Compare revisions