Swift Program to Create an Empty Dictionary

A dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers of any data type like string, integer, etc., that are connected with each value. In Swift, we are allowed to create an empty dictionary. An empty dictionary is a dictionary which does not contain any element or we can say that its size is zero.


let myDicti = [Int:Int]()
let myDicti:[Int:Int]= [:]
let myDicti:[String:Int]= [:]

So to create an empty dictionary you can use any of the given syntaxes. Here it is not necessary that the data type of keys and values should be the same it can be different.


In the following Swift program, we will create an empty dictionary in which the data type of both key and value is the same. And then we will find the size of the dictionary and display it in the output.

import Foundation
import Glibc

// Creating an empty dictionary
var myDict = [String:String]()

// Counting the size of the dictionary
var size = myDict.count

// Displaying set
print("The size of the dictionary is:", size)


The size of the dictionary is: 0


So this is how we can create an empty dictionary. Just like other collections like an empty array, empty set, etc., an empty dictionary also does not contain any element in it and we created an empty dictionary so that we can store key-value pairs later on in the program.

Updated on: 09-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started