Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, dictionaries are mutable, unordered collections of key-value pairs. You can easily add, modify, and remove elements from dictionaries.
Add elements to a dictionary:
To add a new key-value pair to a dictionary, simply assign a value to a new key:
my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
Modify elements in a dictionary:
To modify the value associated with a key, assign a new value to the key:
my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict['b'] = 4 print(my_dict) # Output: {'a': 1, 'b': 4, 'c': 3}
Remove elements from a dictionary:
To remove a key-value pair from a dictionary, use the del
keyword:
my_dict = {'a': 1, 'b': 2, 'c': 3} del my_dict['b'] print(my_dict) # Output: {'a': 1, 'c': 3}
Alternatively, you can use the pop()
method to remove a key-value pair and return its value:
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.pop('b') print(value) # Output: 2 print(my_dict) # Output: {'a': 1, 'c': 3}
Keep in mind that trying to remove a non-existent key using del
or pop()
without providing a default value will result in a KeyError. To avoid this, you can use the pop()
method with a default value, which will be returned if the key is not found:
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.pop('d', None) print(value) # Output: None print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
Or you can use an if
statement to check if the key exists before attempting to remove it:
my_dict = {'a': 1, 'b': 2, 'c': 3} key_to_remove = 'd' if key_to_remove in my_dict: del my_dict[key_to_remove] print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
How to Add Elements to a Python Dictionary:
# Example my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 # Add a new key-value pair
Python Dictionary Add Key Value Pair:
# Example my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 # Add a new key-value pair
Modify Dictionary Values in Python:
# Example my_dict = {'a': 1, 'b': 2} my_dict['a'] = 10 # Modify an existing value
Updating and Changing Dictionary Items in Python:
# Example my_dict = {'a': 1, 'b': 2} my_dict['a'] = 10 # Update an existing value
Remove Key from Dictionary in Python:
pop()
method to remove and return the value for a given key.# Example removed_value = my_dict.pop('b')
Deleting Elements from a Dictionary in Python:
del
keyword or pop()
method to delete key-value pairs.# Example del my_dict['b'] # Delete using del popped_item = my_dict.pop('a') # Delete using pop
Appending Elements to a Dictionary in Python:
# Example my_dict['d'] = 4 # Add a new key-value pair
Dictionary Insert Operation in Python:
# Example my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 # Insert a new key-value pair
Replace Dictionary Values in Python:
# Example my_dict = {'a': 1, 'b': 2} my_dict['a'] = 10 # Replace an existing value
Adding and Updating Dictionary Entries in Python:
# Example my_dict = {'a': 1, 'b': 2} my_dict['c'] = 3 # Add a new key-value pair my_dict['a'] = 10 # Update an existing value