title: Selecting objects in Python dictionaries based on values
body: <p>I am brand-new to Python, having decided to make the jump from Matlab. I have tried to find the answer to my question for days but without success!</p>
  
  <p>The problem: I have a bunch of objects with certain attributes. Note that I am not talking about objects and attributes in the programming sense of the word - I am talking about literal astronomical objects about which I have different types of numerical data and physical attributes for. </p>
  
  <p>In a loop in my script, I go through each source/object in my catalogue, do some calculations, and stick the results in a huge dictionary. The form of the script is like this:</p>
  
  <pre><code>for i in range ( len(ObjectCatalogue) )
  
      calculate quantity1 for source i    
  
      calculate quantity2 for source i 
  
      determine attribute1 for source i 
  
      sourceDataDict[i].update( {'spectrum':quantity1} )
  
      sourceDataDict[i].update( {'peakflux':quantity2} )
  
      sourceDataDict[i].update( {'morphology':attribute1} )
  </code></pre>
  
  <p>So once I have gone through a hundred sources or so, I can, say, access the spectrum for object no. 20 with spectrumSource20 = sourceData[20]['spectrum'] etc.</p>
  
  <p>What I want to do is be able to select all objects in the dictionary based on the value of the keyword 'morphology' say. So say the keyword for 'morphology' can take on the values 'simple' or 'complex'. Is there anyway I can do this without resorting to a loop? I.e. - could I do something like create a new dictionary that contains all the sources that take the 'complex' value for the 'morphology' keyword? </p>
  
  <p>Hard to explain, but using logical indexing that I am used to from Matlab, it would look something like </p>
  
  <pre><code>complexSourceDataDict = sourceDataDict[*]['morphology'=='complex']
  </code></pre>
  
  <p>(where * indicates all objects in the dictionary)</p>
  
  <p>Anyway - any help would be greatly appreciated!</p>