xxxxxxxxxx
diction = {'key':'value'}#Adding a dictionary called diction.
print(diction)
diction['newkey']='newvalue'#Adding the newkey key to diction with its value.
print(diction)
#output: {'key':'value','newkey':'newvalue'}
xxxxxxxxxx
d = {'key':'value'}
print(d)
# {'key': 'value'}
d['mynewkey'] = 'mynewvalue'
print(d)
# {'mynewkey': 'mynewvalue', 'key': 'value'}
xxxxxxxxxx
#adding new key in python
List_of_Students = {"Jim" : "Roll-32"+","+ "Priority-First",
"Yeasin": "Roll-33"+","+ "Priority-2nd",}
List_of_Students.update({"Pinky": "Roll-34"})
for x in List_of_Students:
print(x)
#That will show only the the key
add a key to a dictionary python
xxxxxxxxxx
sampleDictionary = {0: 10, 1:20}
sampleDictionary[2] = 30
print(sampleDictionary)
Output: {0: 10, 1: 20, 2: 30}
add a key to a dictionary python or add key and value to dict
xxxxxxxxxx
student_scores = {'Simon': 45 }
print(student_scores)
# {'Simon': 45}
student_scores['Sara'] = 63
print(student_scores)
# {'Simon': 45, 'Sara': 63}
xxxxxxxxxx
# Basic syntax:
dictionary['new_key'] = 'new_value'
# Example usage:
d = {'a': 1, 'b': 5} # Define dictionary
d['c'] = 37 # Add a new key to the dictionary
print(d)
--> {'a': 1, 'b': 5, 'c': 37}
xxxxxxxxxx
dict = {'key1': 'geeks', 'key2': 'for'}
# using __setitem__ method
dict.__setitem__('newkey2', 'GEEK')
print(dict)