Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Dictionary removeValue()
In this tutorial, we will learn about the Swift Dictionary removeValue() method with the help of examples.
The removeValue() method removes the certain key and its associated value from the dictionary.
Example
// create a dictionary with two elements
var information = ["Charlie": 54, "Harvey": 34]
// remove certain key-value pair using removeValue()
information.removeValue(forKey: "Charlie")
// print updated dictionary
print(information)
// Output: ["Harvey": 34]
removeValue() Syntax
The syntax of the removeValue() method is:
dictionary.removeValue(forKey: key)
Here, dictionary is an object of the Dictionary class.
removeValue() Parameters
The removeValue() method takes one parameter:
key- The key to remove along with its associated value.
removeValue() Return Value
- The
removeValue()method returns the removed value from dictionary.
Example: Swift removeValue()
// create a dictionary with three elements
var numbers = ["one": 1, "Two": 2, "Three": 3]
print("Before:", numbers)
// remove "Three" and it's associated value 3
numbers.removeValue(forKey: "Three")
// print updated dictionary
print("After:", numbers)
Output
Before: ["Two": 2, "one": 1, "Three": 3] After: ["Two": 2, "one": 1]
In the above example, we have created a dictionary named numbers with three key-value pairs.
Here, we have used the removeValue() method to remove the key named "Three" and its associated value 3.
Did you find this article helpful?