append list to dictionary python code example

Example 1: python dictionary of lists append

# Creating an empty dictionary 
myDict = {} 
  
# Adding list as value 
myDict["key1"] = [1, 2] 
  
# creating a list 
lst = ['Geeks', 'For', 'Geeks'] 
  
# Adding this list as sublist in myDict 
myDict["key1"].append(lst) 

# Print Dictionary
print(myDict)

Example 2: python dictionary with list

dictionary = {"one": [1, 2, 3, 4, 5], "two": "something"}
print(dictionary["one"][2])


3

Example 3: python append value to dictionary list

import collections

a_dict = collections.defaultdict(list) # a dictionary key --> list (of any stuff)
a_dict["a"].append("hello")

print(a_dict)
>>> defaultdict(<class 'list'>, {'a': ['hello']})

a_dict["a"].append("kite")

print(a_dict)
>>> defaultdict(<class 'list'>, {'a': ['hello', 'kite']})