import pickle


def read_fusion_pickles(fusion_file_list):
  """
  Given a list of fusion files, read the files and return a list of seeds.
  Note that a single fusion file may contain several seeds, so the final
  list of seeds will usually be longer than the list of fusion files.
  """
  # initialize the list of seeds
  fusion_list = []
  # loop through the file names and extract the seeds
  for fusion_file in fusion_file_list:
    # open the fusion pickle file -- "ab+" opens a file for 
    # both appending and reading in binary mode
    fusion_handle = open(fusion_file, "ab+")
    fusion_handle.seek(0) # start at the beginning of the file
    # step through all of the pickles in the file, adding them
    # to the list
    while True:
      try:
        item = pickle.load(fusion_handle)
        fusion_list.append(item)
      except (EOFError, pickle.UnpicklingError):
        break
    #
    fusion_handle.close()
  # note that fusion_list has the form [s2, s3, s4, n, s2, s3, s4, n, ...],
  # where s2 and s3 are two parts in the seed s4 and n is the birth
  # number of s4 -- however, we only need s4, so let's remove the other
  # items from the list
  seed_list = []
  # read four items at a time
  for (s2, s3, s4, n) in zip(*[iter(fusion_list)] * 4):
    seed_list.append(s4)
  # return the list of s4 items
  return seed_list