title: Finding union and intersection within a dictionary
body: <p>I have a dictionary with a list of movies and actors in the movies, and I'm trying to write two functions: one that finds all actors in any two movies, and another that finds only actors in both of the movies. This is what I have:</p>
  
  <pre><code>def allActors(movie1,movie2):
      dictionary1=makeDictionaryFromFile()
      actor1=[]
      actor2=[]
      if movie1 in dictionary1.keys() and movie2 in dictionary1.keys():
          actor1=dictionary1[movie1]
          actor2=dictionary1[movie2]
          return actor1+actor2
  
  def actorsOverlap(movie1,movie2):
      dictionary1=makeDictionaryFromFile()
      actor1=[]
      actor2=[]
      if movie1 in dictionary1.keys() and movie2 in dictionary1.keys():
          actor1=dictionary1[movie1]
          actor2=dictionary1[movie2]
          actors=[]
          for name in actor1:
              if name in actor2:
                  actors=actors.append([name])
                  return actors
  </code></pre>
  
  <p>The dictionary looks like this:</p>
  
  <pre><code>{'Harry Potter': ['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman'], 'Sense and Sensibility': ['Emma Thompson', 'Alan Rickman', 'Hugh Grant']}
  </code></pre>
  
  <p>so the first should give:</p>
  
  <pre><code>['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman','Emma Thompson', 'Alan Rickman', 'Hugh Grant']
  </code></pre>
  
  <p>(I haven't figured out how to get 'Emma Thompson' and 'Alan Rickman' to only print once)</p>
  
  <p>and the second should give:</p>
  
  <pre><code>['Emma Thompson', 'Alan Rickman']
  </code></pre>
  
  <p>This is part of a longer project, that looks like this so far (with suggested edits):</p>
  
  <pre><code>from Myro import *
  def makeDictionaryFromFile():
     dictionary1={}
     try:
         infile = open("films.txt","r")
         nextLineFromFile = infile.readline().rstrip('\r\n')
         while (nextLineFromFile != ""):
              line = nextLineFromFile.split(",")
              first=line[0]
              dictionary1[first]=line[1:]
              nextLineFromFile = infile.readline().rstrip('\r\n')
      except:
          print ("File not found! (or other error!)")
      return dictionary1
  
  def makeReverseDictionary():
      dictionary1 ={}
      dictionary1 = makeDictionaryFromFile()
      dictionary2 = {}
      for k,vlist in dictionary1.iteritems():
          for v in vlist:
              dictionary2.setdefault(v,[]).append(k)
      return dictionary2
  
   def allActors(movie1,movie2):
      dictionary1 = makeDictionaryFromFile()
      if movie1 in dictionary1.keys() and movie2 in dictionary1.keys():
          return list(set(dictionary1[movie1]+ dictionary1[movie2]))
      return None
  
  def actorsOverlap(movie1,movie2):
      dictionary1 = makeDictionaryFromFile()
      if movie1 in dictionary1.keys() and movie2 in dictionary1.keys():
          return list(set(dictionar1.values) &amp; set(dictionary2.values()))
  
  def coactors(actor):
      dictionary2 = makeDictionaryFromFile()
      (some code)
  
  def menu():
      answer=askQuestion("choose one",["create actor-title dictionary", "create title-actor dictionary", "find all actors in two movies", "find all overlapping actors in two movies","find all actors not in both movies","find all co-actors of an actor","Nothing, I wish to quit"])
      if answer == ("create actor-title dictionary"):
          answer = 1
      elif answer == ("create title-actor dictionary"):
          answer = 2
      elif answer == ("find all actors in two movies"):
          answer = 3
      elif (answer == "find all overlapping actors in two movies"):
          answer = 4
      elif (answer == "find all co-actors of an actor"):
          answer = 5
      elif (answer == "Nothing, I wish to quit"):
          answer = 6
      return answer
  
  def main():
      choice = menu()
      while (choice != 6):
          if (choice == 1):
              dictionary=makeDictionaryFromFile()
              print (dictionary)
          elif (choice == 2):
              dictionary=makeReverseDictionary()
              print (dictionary)
          elif (choice == 3):
              movie1=input("first movie")
              movie2=input("second movie")
              actors=allActors(movie1, movie2)
              print (actors)
          elif (choice == 4):
              movie1=input("first movie")
              movie2=input("second movie")
              actors=actorsOverlap(movie1,movie2)
              print (actors)
          elif (choice == 5):
              actor=input("actor's name")
              try:
                  coactors=coactors(actor)
                  print (coactors)
              except:
                   print ("Error. Check spelling and try again.")
          choice = menu()
  </code></pre>