Problems appending lists to dictionary in python

I’m using this code to append values into a python dictionary:

df = {}

def set_key(dictionary, key, value):
     if key not in dictionary:
         dictionary[key] = value
     elif type(dictionary[key]) == list:
         dictionary[key].append(value)
     else:
         dictionary[key] = [dictionary[key], value]

list1 = ['a', 'b', 'c']
set_key(df, 'extra_contents', list1)
print(df)

output> {'extra_contents': ['a', 'b', 'c']}

The problem is that when I try to execute this function again the following happens:

list2 = ['d', 'e']
set_key(df, 'extra_contents', list2)
print(df)

output> {'extra_contents': ['a', 'b', 'c', ['d', 'e']]}

Here’s my desired output:

{'extra_contents': [['a', 'b', 'c'], ['d', 'e']]}

How can I turn the current output to the desired output? If possible only by changing the function I’m using.

My main goal here is to later turn this dict into a pandas dataframe, therefore, each list would be a row in “extra_contents” column.

Answer

I think this is what you want :

def set_key(dictionary, key, value):
      if key in dictionary:
          dictionary[key].append(value)
      else:
          dictionary[key]=[value]