'Python function that adds nested list to value [duplicate]

I'm trying to write a function that adds a list of lists to a key in a dictionary based on a condition. The dataframe looks similar to the following:

labels        TEXT
 -1    this is a sentence...
 -1    this is a sentence...
  1    this is a sentence...
  2    this is a sentence...
  2    this is a sentence...
  3    this is a sentence...
  3    this is a sentence...

I want every key in the dictionary to have values associated with them based on the df.labels I have.

Is there a way to append the string to the values of a dictionary? This is the code I have so far.

def function(df):
    
    my_nums = list(set(df.labels.tolist()))
    
    my_dict = {}
    for i in range(len(my_nums)):
        for k in range(len(df)):
            if my_nums[i] == df.labels[k]:
                # add the list of lists to a key in the dictionary
                # my_dict[label_nums[i]] = df.TEXT[k] doesn't quite work.
           
    return my_dict


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source