title: Multiple values to one key in python
body: <p>So my main goal is simple, I want multiple values to be returned when using a single key. However I'm getting errors and confusing behavior. I am new to Python, so I fully expect there to be a simple reason for this issue. </p>
  
  <p>I have a list of objects, <code>list</code> which only contains the index of the object. i.e. </p>
  
  <pre><code>1
  2
  3
  4 
  etc.. 
  </code></pre>
  
  <p>and a file containing the groups that each of the objects belong to, listed in the same order. The file is a single value for n lines (n being the length of the list of objects as well.) i.e. the file looks like this:</p>
  
  <pre><code>2
  5
  2
  4
  etc..
  </code></pre>
  
  <p>meaning the first object belongs in group 2, the second object in group 5, the third in group 2 and fourth in group 4. This file will change depending on my input files.  I have attempted the two following suggestions (that I could find). </p>
  
  <p><strong>EDIT:</strong> my end goal: to have a dictionary with group numbers as the keys and the objects in the groups as the values.</p>
  
  <p>I looked to this StackOverflow <a href="https://stackoverflow.com/questions/3199171/append-multiple-values-for-one-key-in-python-dictionary">question</a> first for help since it is so similar, and ended up with this code:</p>
  
  <pre><code>def createdDict(list, file): 
  
      f = open(file, 'r')
      d={}
      i=0 
  
      for line in f:
          groupIndex = int(line)
          if groupIndex in d:
              d[groupIndex].append(list[i])
          else:
              d[groupIndex] = list[i]
          i +=1
      print d
      f.close()
  </code></pre>
  
  <p>And this error:</p>
  
  <pre><code>AttributeError: 'Element' object has no attribute 'append'
  </code></pre>
  
  <p><code>d[groupIndex]</code> is just a dictionary and its key and <code>groupIndex</code> should also just be an integer.. not an object from a class I created earlier in the script. (Why is this error showing up?)</p>
  
  <p>I then revised my code after coming upon this <a href="https://stackoverflow.com/questions/10844282/appending-values-to-a-key-if-key-already-exists-python-jython?rq=1">other question</a> to be like the following, since I thought this was an alternative way to accomplish my task. My code then looked like this:</p>
  
  <pre><code>def createdDict(list, file): 
  
      f = open(file, 'r')
      d={}
      i=0 
  
      for line in f:
          groupIndex = int(line)
          if groupIndex in d:
              d.setdefault('groupIndex', []).append(list[i])
          else:
              d[groupIndex] = list[i]
          i +=1
      print d
      f.close()
  </code></pre>
  
  <p>This code snippet doesn't end in an error or what I want, but rather (what I believe) are the last objects in the groups... so <code>print d</code> gives me the key and the last object placed in the group (instead of the desired: ALL of the objects in that group) and then terminal randomly spits out <code>groupIndex</code> followed by all of the objects in <code>list</code>. </p>
  
  <p>My question: what exactly am I missing here? People upvoted the answers to the questions I linked, so they are most likely correct and I am likely implementing them incorrectly. I don't need a correction to both procedures, but the most efficient answer to my problem of getting multiple values attached to one key. What would be the most pythonic way to accomplish this task?</p>
  
  <p><strong>EDIT 2:</strong> if this helps at all, here is the class that the first method is referencing the error too. I have no idea how it defined any part of this code as a part of this class. I haven't really developed it yet, but I'm all for an answer, so if this helps in locating the error:</p>
  
  <pre><code>class Element(object):
      def __init__(self, globalIndex):
          self.globalIndex = globalIndex
      def GetGlobalIndex (self):
          return self.globalIndex
  </code></pre>
  
  <p><code>globalIndex</code> is a separate index of objects (Elements). with my current problem, I am taking a list of these Elements (this is the list mentioned earlier) and grouping them into smaller groups based upon my file (also mentioned earlier). Why I thought it shouldn't matter, the list is essentially a counting up of integers... How would it mess with my code? </p>