title: Return values of .get() method in for loop - need to change my thinking?
body: <p>I am taking an algorithms class on Udacity. I've hit a sticking point in the class where I was not able to solve the problem. Instead I went into the discussion forums to look at other answers for help. However when reading the code I could not figure out how it worked after much trying to do so. Any help GREATLY appreciated</p>
  
  <p>What I am trying to understand is this block of code:</p>
  
  <pre><code>graph = [(0, 1), (1, 5)]
  def get_degree(graph):
      degree = {}
      for x, y in graph:
          degree[x] = degree.get(x, 0) + 1
          degree[y] = degree.get(y, 0) + 1
      return degree
  </code></pre>
  
  <p>I have looked at the Python docs and watched a couple youtube videos to get an understanding on dictionaries, tuples and the get method. This is where I'm stuck:</p>
  
  <pre><code>def get_degree(graph):
      degree = {}
      for x, y in graph:
          print degree
          degree[x] = degree.get(x, 0) + 1
          print x, y
          print degree.get(x, 0) + 1
          print degree[x]
          print degree
  
          degree[y] = degree.get(y, 0) + 1
          print degree
      return ""
  
  graph = [(0, 1), (1, 5)]
  
  get_degree(graph)
  </code></pre>
  
  <p>When I call this fuction my output is as follows:</p>
  
  <pre class="lang-none prettyprint-override"><code>{}
  0 1
  2
  1
  {0: 1}
  {0: 1, 1: 1}
  {0: 1, 1: 1}
  1 5
  3
  2
  {0: 1, 1: 2}
  {0: 1, 1: 2, 5: 1}
  </code></pre>
  
  <p>Now he's my process for understanding this:</p>
  
  <ol>
  <li>Prints out degree which is an empty dict so it get {}</li>
  <li>prints out x, y which is the first tuple so i get 0 1</li>
  </ol>
  
  <p>HELP NEEDED HERE* 3. this statement prints out 2 which confuses me because when calling degree.get(x, 0) + 1 I would expect to get a value of 1. The way I see it when it called degree.get(x,0) it should see that the first x is 0 from the first tuple (0, 1) then would return that 0 and add 1 to it giving me a value of 1.</p>
  
  <p>HELP NEEDED HERE* 4. Now I print degree[x] this should be the same value as above because "degree[x] = degree.get(x, 0) + 1" However the output from this print statement is 1</p>
  
  <p>HELP NEEDED HERE* 5. Now I ask it to print the dictionary thus far which I would expect it to be {degree[x]} because at this point I have only supply that, however my output is {0, 1}</p>
  
  <p>If someone can help me think about this correctly I think I could figure the rest out on my own.</p>