Jack
0
Q:

python dictoinary add value

dict[key] = value
14
student_scores = {'Simon': 45  }
print(student_scores)
# {'Simon': 45}
student_scores['Sara'] = 63
print(student_scores)
# {'Simon': 45, 'Sara': 63}
2
>>> d1 = {1: 1, 2: 2}
>>> d2 = {2: 'ha!', 3: 3}
>>> d1.update(d2)
>>> d1
{1: 1, 2: 'ha!', 3: 3}
4
# This automatically creates a new element where
# Your key = key, The value you want to input = value
dictionary_name[key] = value
1
dict = {'key1':'geeks', 'key2':'for'} 
print("Current Dict is: ", dict) 
  
# adding key3 
dict.update({'key3':'geeks'}) 
print("Updated Dict is: ", dict) 
  
# adding dict1 (key4 and key5) to dict 
dict1 = {'key4':'is', 'key5':'fabulous'} 
dict.update(dict1) 
print(dict) 
  
# by assigning  
dict.update(newkey1 ='portal') 
print(dict) 
4
# Append to Dictionary in Python

# Let's say we had the following dictionary:

languages = {'#1': "Python", "#2": "Javascript", "#3": "HTML"}

# There are two ways to add a key-and-value set to this dictionary

# Number 1: By .update() method

languages.update({"#4": "C#"}) # Adds a #4 key-and-value set

#--------------------------------------------

# Number 2: The define-key method

# This is the easier one

languages['#4'] = 'C#'

# ^^ Just updates a key of #4 to C#, or adds it in this case

0
# Let's create a dictionary, the functional way 
  
# Create your dictionary class 
class my_dictionary(dict): 
  
    # __init__ function 
    def __init__(self): 
        self = dict() 
          
    # Function to add key:value 
    def add(self, key, value): 
        self[key] = value 
  
# Main Function 
dict_obj = my_dictionary() 
  
dict_obj.add(1, 'Geeks') 
dict_obj.add(2, 'forGeeks') 
  
print(dict_obj) 
-2

New to Communities?

Join the community