| by Arround The Web | No comments

Swift Dictionary – RemoveAll() and RemoveValue()

In this Swift guide, we will remove the elements from a Swift Dictionary using the removeAll() and removeValue() methods.

If we want to create a Dictionary in Swift, we have to specify the datatype of the Key and Value pair while creating a Dictionary. Both the data types of the key-value pairs can be the same or different.

We will discuss it one by one.

RemoveAll() Method

The removeAll() method in Swift Dictionary deletes all the key-value pairs. It returns an empty Dictionary-[:].

Syntax:

swift_dictionary.removeAll()

 
Where the swift_dictionary is the input dictionary.

Example 1:

We create a swift dictionary that has 7 key-value pairs and use the removeAll() method to delete all the elements in that dictionary.

//create a swift Dictionary
var swift_dictionary = [1:"Java",2:"PHP",3:"Scipy",4:"Pandas",5:"Pandas",6:"Java",7:"Pandas"]

print("Actual Dictionary - \(swift_dictionary)")

//filter the swift_dictionary such that value is Pandas
print("Pandas values - \(swift_dictionary.filter{ $0.value == "Pandas" })")

 
Output:

Explanation:

Line 2


We created a dictionary named swift_dictionary with 7 key-value pairs. Here, key is the Integer type and value is the String type.

Line 7


Here, we use the removeAll() method to remove all the elements in the swift_dictionary.

Line 9


When we print the swift_dictionary, it returns empty.

Example 2:

We create a swift dictionary that has 2 key-value pairs and use the removeAll() method to delete all the elements in that dictionary.

//create a swift Dictionary
var swift_dictionary = [1:100,2:200]
 
print("Actual Dictionary - \(swift_dictionary)")

//remove all elements from the swift_dictionary
swift_dictionary.removeAll()

print("Dictionary after Removing all elements - \(swift_dictionary)")

 
Output:

Explanation:

Line 2


We created a dictionary named swift_dictionary with 2 key-value pairs. Here, key is the Integer type and value is also the Integer type.

Line 7


Here, we use the removeAll() method to remove all the elements in the swift_dictionary.

Line 9


When we print the swift_dictionary, it returns empty.

RemoveValue() Method

The removeValue() method in Swift Dictionary deletes a particular key-value pair based on the key provided. It takes a key as a parameter.

Syntax:

swift_dictionary.removeValue(forKey:Key)

 
Where the swift_dictionary is the input dictionary.

Parameter:

Key is the Key present in the swift dictionary. The key_value pair is deleted associated with this Key.

Example 1:

We create a swift dictionary that has 7 key-value pairs and use the removeValue() method to delete a pair with key-4.

//create a swift Dictionary
var swift_dictionary = [1:100,2:200,3:200,4:400,5:12,6:34,7:100]
 
print("Actual Dictionary - \(swift_dictionary)")

//remove pair with key-4
swift_dictionary.removeValue(forKey:4)

print("Remaining elements - \(swift_dictionary)")

 
Output:

Explanation:

Line 2


We created a dictionary named swift_dictionary with 7 key-value pairs. Here, key is the Integer type and value is also the Integer type.

Line 7


Here, we use the removeValue() method to remove the pair with key as 4.

Line 9


When we print the swift_dictionary, it returns the key-value pairs except key-4.

Example 2:

We create a swift dictionary that has 4 key-value pairs and use the removeValue() method to delete a pair with key-”sravan”.

//create a swift Dictionary
var swift_dictionary = ["sravan":100,"souji":3,"Megna":20,"Rakhesh":23]
 
print("Actual Dictionary - \(swift_dictionary)")

//remove pair with key-"sravan"
swift_dictionary.removeValue(forKey:"sravan")

print("Remaining elements - \(swift_dictionary)")

 
Output:

Explanation:

Line 2


We created a dictionary named swift_dictionary with 7 key-value pairs. Here, key is the String type and value is the Integer type.

Line 7


Here, we use the removeValue() method to remove the pair with the key as “sravan”.

Line 9


When we print the swift_dictionary, it returns the key-value pairs except key-”sravan”.

Conclusion

In Swift, if you want to remove the entire elements from the entire Dictionary, you can use the removeAll() method. It doesn’t take any parameters. If you want to remove a particular key-value pair, the removeValue() method is used. The removeValue() method in Swift Dictionary deletes a particular key-value pair based on the key provided. It takes a key as a parameter.

Share Button

Source: linuxhint.com

Leave a Reply