Get the Size of a Dictionary in Swift

Use the count property to get the number of key-value pairs in a Swift dictionary. The returned value is an Int.

For example, a dictionary containing three keys has a count of 3, even when one or more values are collections or other complex objects.

Swift Dictionary count Syntax

The syntax to count the elements in a dictionary is given below.

</>
Copy
dictionary_name.count

The count property reports how many key-value pairs are currently stored in the dictionary. It does not report the dictionary’s memory usage in bytes.

Example 1 – Get Size of a Swift Dictionary

In this example, we will create a Swift Dictionary with some initial values and find its size using count function.

main.swift

</>
Copy
var myDictionary:[String:Int] = ["Mohan":75, "Raghu":82, "John":79]

var size = myDictionary.count

print( "myDictionary size: \(size)" )

Output

myDictionary size: 3

The result is 3 because the dictionary stores three key-value pairs: Mohan, Raghu, and John.

Example 2 – Get Size of Empty Dictionary

In this example, we will create an empty dictionary and find its size using count function. Since the dictionary is empty, the count property returns 0.

main.swift

</>
Copy
var myDictionary = [String:Int]()

var size = myDictionary.count

print( "myDictionary size: \(size)" )

Output

myDictionary size: 0

Check Whether a Swift Dictionary Is Empty

You can compare count with zero, but Swift also provides the isEmpty property. Use isEmpty when the only question is whether the dictionary contains any entries.

</>
Copy
let scores: [String: Int] = [:]

if scores.isEmpty {
    print("The dictionary is empty.")
}

print("Number of entries: \(scores.count)")

Output

The dictionary is empty.
Number of entries: 0

How Dictionary Updates Change the count Value

The value of count changes when you insert or remove a key-value pair. Updating the value for an existing key does not increase the count because the key already exists.

</>
Copy
var capitals = [
    "France": "Paris",
    "Japan": "Tokyo"
]

print(capitals.count)

capitals["India"] = "New Delhi"
print(capitals.count)

capitals["Japan"] = "Tokyo Metropolis"
print(capitals.count)

capitals.removeValue(forKey: "France")
print(capitals.count)

Output

2
3
3
2

Count Key-Value Pairs in a Nested Swift Dictionary

For a nested dictionary, the outer dictionary’s count includes only its direct keys. It does not recursively count entries stored inside nested dictionaries.

</>
Copy
let departments = [
    "Engineering": ["iOS": 4, "Backend": 6],
    "Design": ["Product": 3]
]

print(departments.count)
print(departments["Engineering"]?.count ?? 0)

Output

2
2

The outer dictionary has two department keys. The dictionary stored under Engineering also has two keys.

Dictionary Entry Count Versus Memory Size in Swift

In Swift, dictionary “size” usually means the number of stored key-value pairs, which is what count returns. It does not measure the actual memory footprint of the dictionary.

Memory usage depends on factors such as key and value types, storage capacity, hashing overhead, copy-on-write storage, and runtime implementation details. Therefore, do not use count when you need a byte-level memory measurement.

Swift Dictionary count FAQs

How do I check the size of a dictionary in Swift?

Read the dictionary’s count property. For example, scores.count returns the number of key-value pairs stored in scores.

What does count return for an empty Swift dictionary?

It returns 0. You can also use isEmpty when you only need a Boolean result.

Does replacing a dictionary value increase its count?

No. Replacing the value associated with an existing key leaves the count unchanged. Adding a new key increases the count by one.

Does Swift Dictionary count measure memory usage?

No. It measures entries, not bytes. Actual memory use depends on the stored types and Swift’s internal storage.

Swift Dictionary Size Editorial QA Checklist

  • Confirm that every use of “size” is explained as the number of key-value pairs.
  • Verify that empty dictionary examples return 0.
  • Check that inserting a new key increases count, while replacing an existing value does not.
  • Keep entry count separate from memory-footprint discussions.
  • Use isEmpty when the example only checks whether a dictionary has entries.

Summary of Swift Dictionary count

Use the count property to get the number of key-value pairs in a Swift dictionary. An empty dictionary has a count of 0, and isEmpty is the clearer choice for an emptiness check. In this Swift Tutorial, we have learned how dictionary insertion, replacement, removal, and nesting affect the reported count.