Since train_dataset is an instance of ImageFolder which is really just an instance of DatasetFolder, you can access it directly e.g.,
targetlst = list(train_dataset.targets)

If you don’t mutate targetlst you can likely get away with using train_dataset.targets directly.
Relevant code in torchvision:
  
      github.com
  
  
    pytorch/vision/blob/f5843099d895e72c27ffa9d29cc91dd8df7f3832/torchvision/datasets/folder.py#L154 1

                                        target_transform=target_transform)
    classes, class_to_idx = self.find_classes(self.root)
    samples = self.make_dataset(self.root, class_to_idx, extensions, is_valid_file)


    self.loader = loader
    self.extensions = extensions


    self.classes = classes
    self.class_to_idx = class_to_idx
    self.samples = samples
    self.targets = [s[1] for s in samples]


@staticmethod
def make_dataset(
    directory: str,
    class_to_idx: Dict[str, int],
    extensions: Optional[Tuple[str, ...]] = None,
    is_valid_file: Optional[Callable[[str], bool]] = None,
) -> List[Tuple[str, int]]:
    """Generates a list of samples of a form (path_to_sample, class).