< Return to Video

Dictionary iteration | Intro to CS - Python | Khan Academy

  • 0:00 - 0:01
    - [Instructor] How do we iterate over
  • 0:01 - 0:03
    the items in a dictionary?
  • 0:03 - 0:05
    There's no indices like enlists,
  • 0:05 - 0:08
    so we can't use for I in range,
  • 0:08 - 0:11
    but we can use a for each loop,
  • 0:11 - 0:13
    we have the keyword for
  • 0:13 - 0:16
    the loop variable name. the keyword in,
  • 0:16 - 0:20
    and then the dictionary name
    followed as always by a colon.
  • 0:21 - 0:23
    But what do you think
    the loop variable holds
  • 0:23 - 0:24
    on each iteration?
  • 0:25 - 0:27
    When we iterate over a dictionary,
  • 0:27 - 0:30
    the loop variable holds the next key.
  • 0:30 - 0:34
    So we name our loop variable
    descriptively based on
  • 0:34 - 0:37
    what the keys in the dictionary represent.
  • 0:37 - 0:39
    Here, my dictionary keeps
    track of word frequencies
  • 0:39 - 0:40
    in a text.
  • 0:40 - 0:45
    So my keys are all words,
    and my values are all counts.
  • 0:45 - 0:48
    Python iterates over
    dictionaries in insertion order,
  • 0:48 - 0:51
    meaning the order the items were added.
  • 0:51 - 0:53
    So on the first iteration,
  • 0:53 - 0:56
    the variable word will hold the key it.
  • 0:56 - 0:59
    On the second iteration,
    it'll hold the key is,
  • 1:00 - 1:02
    and on the third iteration,
  • 1:02 - 1:04
    it'll hold the key, what.
  • 1:04 - 1:07
    And now that we've reached
    the end of the dictionary,
  • 1:07 - 1:08
    the loop terminates.
  • 1:10 - 1:14
    If I want the associated
    value inside the loop body,
  • 1:14 - 1:16
    I just access it using the key.
  • 1:16 - 1:20
    So now this prints out its value, two,
  • 1:20 - 1:21
    is's value two.
  • 1:21 - 1:23
    And then what's value, one.
  • 1:25 - 1:27
    What if I wanna know what percentage
  • 1:27 - 1:30
    of words were used more than once?
  • 1:30 - 1:31
    I need to keep track of the number
  • 1:31 - 1:34
    of words whose count is greater than one.
  • 1:34 - 1:36
    The total number of unique words
  • 1:36 - 1:38
    is just the length of the dictionary.
  • 1:38 - 1:41
    I'm using a very small dictionary
    here so we can trace it.
  • 1:41 - 1:44
    But you can imagine this
    analysis will be a lot cooler
  • 1:44 - 1:46
    with a dictionary with more words.
  • 1:46 - 1:50
    Notice that my loop body only
    really cares about the values
  • 1:50 - 1:52
    and never uses the key on its own.
  • 1:54 - 1:55
    For situations like this,
  • 1:55 - 2:00
    Python provides two dictionary
    methods, values and items.
  • 2:00 - 2:04
    There is also a keys method,
    though we don't need it here.
  • 2:04 - 2:08
    This means we can iterate over
    a dictionary in three ways,
  • 2:08 - 2:11
    by key, by value, and by item
  • 2:11 - 2:13
    or key and value.
  • 2:14 - 2:16
    We can achieve all of the same things
  • 2:16 - 2:18
    just by iterating by key,
  • 2:18 - 2:21
    but these alternate
    syntaxes help make our code
  • 2:21 - 2:24
    a bit cleaner depending on
    what we're trying to do.
  • 2:25 - 2:26
    So back to our program,
  • 2:26 - 2:28
    we can change it to iterate by value.
  • 2:28 - 2:31
    We call the values
    method on our dictionary,
  • 2:31 - 2:35
    and then we update our loop
    variable since now it represents
  • 2:35 - 2:37
    the next value in the dictionary.
  • 2:40 - 2:42
    Okay, how about iterating by item?
  • 2:42 - 2:45
    We call the items method
    on our dictionary,
  • 2:45 - 2:48
    and then the loop variable
    holds the next item,
  • 2:48 - 2:51
    meaning the next key value pair.
  • 2:51 - 2:53
    But what data type is that?
  • 2:53 - 2:55
    In Python, we call this a tuple.
  • 2:55 - 2:59
    A tuple is just the immutable
    form of a list defined
  • 2:59 - 3:01
    with parentheses instead
    of square brackets,
  • 3:01 - 3:05
    we can index into a tuple,
    but we can't mutate it.
  • 3:05 - 3:07
    So if we wanna access the key,
  • 3:07 - 3:09
    it'll be the first element in the tuple.
  • 3:09 - 3:11
    And the value is the second element.
  • 3:12 - 3:14
    A bit annoying to have to write that out
  • 3:14 - 3:17
    because the key will
    always be at index zero
  • 3:17 - 3:20
    and the value will always be at index one.
  • 3:20 - 3:22
    Fortunately, Python gives
    us a shortcut called
  • 3:22 - 3:24
    variable unpacking.
  • 3:24 - 3:27
    We can unpack an Iterable
    type, like a tuple
  • 3:27 - 3:30
    or a list into several
    different variables,
  • 3:30 - 3:33
    all in a single assignment.
  • 3:33 - 3:35
    So we can unpack our
    item topple into its key
  • 3:35 - 3:37
    and value by putting a comma in
  • 3:37 - 3:39
    between the two variable names.
  • 3:39 - 3:41
    Word will contain the first element
  • 3:41 - 3:43
    and count will contain the second.
  • 3:43 - 3:44
    And to make this even easier,
  • 3:44 - 3:47
    we can inline this unpacking
    into our for statement.
  • 3:49 - 3:49
    On each iteration,
  • 3:49 - 3:52
    the loop variable word
    contains the next key,
  • 3:52 - 3:55
    and the loop variable count
    contains the next value.
  • 3:55 - 3:57
    Be extra careful with the syntax,
  • 3:57 - 4:00
    because it's easy to forget
    the comma or the dot items
  • 4:00 - 4:02
    and end up with a weird error.
  • 4:04 - 4:07
    We can calculate the same
    result, just iterating by key.
  • 4:07 - 4:09
    But if we need both the key
  • 4:09 - 4:11
    and the value in the loop body,
  • 4:11 - 4:14
    iterating by item often
    makes this a lot cleaner.
  • 4:17 - 4:20
    So how do these methods actually work?
  • 4:20 - 4:23
    The keys, values and
    items methods all return
  • 4:23 - 4:25
    a special dictionary view type.
  • 4:25 - 4:28
    This is an Iterable type,
    meaning we can iterate over it,
  • 4:28 - 4:30
    but it's not quite a list.
  • 4:30 - 4:35
    Instead it's an optimized view
    into the existing dictionary.
  • 4:35 - 4:37
    You can think of it like a
    special little window into
  • 4:37 - 4:39
    the computer's memory where we can peek in
  • 4:39 - 4:42
    on specific parts of the dictionary.
  • 4:42 - 4:44
    Like the values method gives us a window
  • 4:44 - 4:47
    into just the dictionary values.
  • 4:47 - 4:51
    We can cast a dictionary
    view to a list if we need
  • 4:51 - 4:53
    to index into it,
  • 4:53 - 4:55
    but if we're just iterating over it,
  • 4:55 - 4:57
    the cast is not necessary.
  • 4:57 - 4:59
    In fact, it's not optimal.
  • 4:59 - 5:02
    As soon as we cast this
    dictionary view to a list,
  • 5:02 - 5:05
    the computer copies
    over all of these values
  • 5:05 - 5:07
    to a new location in memory.
  • 5:07 - 5:10
    Whereas if we just use the
    dictionary view directly,
  • 5:10 - 5:11
    we're just peeking at the memory
  • 5:11 - 5:13
    where the original dictionary is stored.
  • 5:14 - 5:16
    Implementation aside,
  • 5:16 - 5:19
    the big takeaway is that we
    can iterate over a dictionary
  • 5:19 - 5:23
    by key, by value, and by item.
  • 5:23 - 5:25
    The choice is just up
    to what data you need.
Title:
Dictionary iteration | Intro to CS - Python | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
05:26

English subtitles

Revisions