Python Dictionary Count using Various Methods

In this Python tutorial, we will study the Python dictionary Count using some examples in python.

In Python, a dictionary is a data structure that stores key-value pairs. The number of keys in a dictionary can be determined using 6 methods.

  • Using the len() function
  • Using the for loop
  • Using the dict.keys()
  • Using the dict.items()
  • Using the list comprehension
  • Using the enumerate() function

Get Python Dictionary Count

Here we will discuss and understand how to determine the number or count of Dictionary keys.

Let us understand the first method using the len() function in Python.

Method-1: Python Dictionary Count using the len() function

In this method, we will use the len() function to determine the number of keys in a dictionary. The len() function returns the number of items in a list-like object, including dictionaries.

# create a dictionary with keys and values
countries = {'USA': 1, 'United Kingdom': 2, 'Brazil': 3}

# find the number of keys in the dictionary using the len() function
count = len(countries)

# print the count
print(count)
Python dictionary count using the len function
Python dictionary count using the len() function

Read: Python dictionary pop

Method-2: Python Dictionary Count using the for loop

In this method, we will use a for loop to loop through the keys in the dictionary and increment a counter variable for each key. This will give us the number of keys in the dictionary.

# create a dictionary with keys and values
countries = {'USA': 1, 'United Kingdom': 2, 'Brazil': 3}

# initialize a variable to count the number of keys
count = 0

# loop through the keys in the dictionary
for key in countries:
    # increment the count for each key
    count += 1

# print the count
print(count)

The above code demonstrates how to count the number of keys in a dictionary using a for loop.

  1. First, a dictionary named countries is created with key-value pairs.
  2. Then, a variable named count is initialized to store the number of keys in the dictionary.
  3. A for loop is used to loop through the keys in the countries dictionary using the for key in countries: syntax.
  4. Inside the for loop, the count variable is incremented by 1 for each iteration.
  5. After the for loop, the value of the count is printed to the console using the print() function.
Python dictionary count using the for loop
Python dictionary count using the for loop

Read: Python dictionary extend

READ:  Python Dictionary of sets [6 Methods to create]

Method-3: Python Dictionary Count using the dict.keys()

In this method, we will use the dict.keys() method to get a list-like object containing the keys of the dictionary, and use the len() function to determine the number of keys.

# create a dictionary with keys and values
countries = {'USA': 1, 'United Kingdom': 2, 'Brazil': 3}

# find the number of keys in the dictionary using the dict.keys() method and the len() function
count = len(countries.keys())

# print the count
print(count)

The above code demonstrates how to count the number of keys in a dictionary using the dict.keys() method and the len() function.

  • First, a dictionary named countries are created with key-value pairs.
  • Then, the dict.keys() method is used to extract the keys from the dictionary as a list.
  • The len() function is used to determine the number of items in the list, which gives us the number of keys in the countries dictionary.
  • The result is then stored in a variable named count.
  • Finally, the value of the count is printed to the console using the print() function.
Python dictionary count using the keys() and len()
Python dictionary count using the keys() and len()

Read: Python Dictionary of sets

Method-4: Python Dictionary Count using the dict.items()

In this method, we will use the dict.items() method to get a list-like object containing the items of the dictionary, and use the len() function to determine the number of items, which will give us the number of keys in the dictionary.

# create a dictionary with keys and values
countries = {'USA': 1, 'United Kingdom': 2, 'Brazil': 3}

# find the number of keys in the dictionary using the dict.items() method and the len() function
count = len(countries.items())

# print the count
print(count)

The above code demonstrates how to count the number of keys in a dictionary using the dict.items() method and the len() function.

  • First, a dictionary named countries are created with key-value pairs.
  • Then, the dict.items() method is used to extract the key-value pairs from the dictionary as a list of tuples.
  • The len() function is used to determine the number of tuples in the list, which gives us the number of key-value pairs in the countries dictionary.
  • The result is then stored in a variable named count.
  • Finally, the value of the count is printed to the console using the print() function.
Python dictionary count using the items and len
Python dictionary count using the items and len

Read: Python dictionary filter

READ:  How to Append String to Beginning of List Python | How to Append to Start of String Python

Method-5: Python Dictionary Count using the list comprehension

In this method, we will use a list comprehension to extract the keys from the dictionary and get the length of the resulting list using the len() function. This will give us the number of keys in the dictionary.

# Define the dictionary named country
country = {'1': 'USA', '2': 'United Kingdom','3': 'Asia'}

# Use list comprehension to extract the keys from the dictionary and store it in the result list
result = len([key for key in country])

# Print the result, which is the number of keys in the dictionary
print("Number of keys in the dictionary:", result)

The above code uses a list comprehension to count the number of keys in a dictionary.

  • First, a dictionary named country are created with key-value pairs.
  • Then, a list comprehension is used to extract the keys from the dictionary. The list comprehension [key for key in country] creates a new list result that contains all the keys from the country dictionary.
  • Finally, the len() function is used to determine the number of items in the list, which gives us the number of keys in the country dictionary.
  • The result is then printed to the console using the print() function.
Python dictionary count using the list comprehension
Python dictionary count using the list comprehension

Read: Python dictionary contains

Method-6: Python Dictionary Count using the enumerate() function

In this method, we will use enumerate() to loop over the keys in a dictionary and keep track of the number of keys. By using len() with enumerate(), we can find the number of keys in a dictionary.

# create a dictionary with keys and values
countries = {'USA': 1, 'United Kingdom': 2, 'Brazil': 3}

# find the number of keys in the dictionary using the enumerate() function and the len() function
count = len([key for index, key in enumerate(countries)])

# print the count
print(count)

The above code demonstrates how to count the number of keys in a dictionary using the enumerate() function.

  • First, a dictionary named countries is created with key-value pairs.
  • Then, the enumerate() function is used to enumerate the keys in the countries dictionary.
  • The enumerate() function returns a list of tuples, where each tuple contains an index and a key from the dictionary.
  • The length of the list returned by enumerate() is then found using the len() function and stored in a variable named count.
  • Finally, the value of the count is printed to the console using the print() function.
Python dictionary count using the enumerate
Python dictionary count using the enumerate

You may also like to read the following Python tutorials.

READ:  Find the Sum of Digits of a Number in Python using For Loop

In this Python tutorial, we have learned the implementation of Python Dictionary Count using the following method in python:

  • Using the len() function
  • Using the for loop
  • Using the dict.keys()
  • Using the dict.items()
  • Using the list comprehension
  • Using the enumerate() function