Answers for "python append value to dictionary list"

3

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)
Posted by: Guest on June-22-2020
1

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']})
Posted by: Guest on May-24-2021
2

dictionary append value python

d = {1:2}
d.update({2: 4})
print(d) # {1: 2, 2: 4}
Posted by: Guest on December-29-2020

Code answers related to "python append value to dictionary list"

Python Answers by Framework

Browse Popular Code Answers by Language