Program to Count the Number of Null elements in a List

Python Program to Count the Number of Null elements in a List

In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop
Python lists are similar to arrays in C or Java. A list is a collection of elements.
The primary distinction between a list and an array is that a list can store multiple types of elements, whereas an array can only store one type.

A list can contain “null elements” in addition to numbers, characters, strings, and so on.

Examples:

Example1:

Input:

Given List =["btechgeeks","hello", "", 123, "" ]

Output:

The Number of Null Elements in the above given list =  2

Example 2:

Input:

Given List =["", "btechgeeks", "good ", "",  "hello",  "",  "",1,2,9 ]

Output:

The Number of Null Elements in the above given list =  4

Program to Count the Number of Null elements in a List

Below are the ways to Count the Number of Null elements in a given list.

Method #1: Using For Loop (Static input)

Approach:

  • Give the list as static input and store it in a variable.
  • Take a variable to say ‘count’ and initialize its value with ‘0’
  • Loop from 0 to the length of the above-given list using For Loop.
  • Inside the loop, check whether the value of the iterator is Null or not using the if conditional statement.
  • If the given condition is true, then increment the above-initialized count value by ‘1’.
  • Print the number of Null elements in a given List by printing the above count value.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["btechgeeks", "hello", "", 123, ""]
# Take a variable say count and initialize it's value with '0'
cou_nt = 0
# Loop from 0 to the length of the above given list using For Loop.
for itetor in range(len(gvn_lst)):
 # Inside the loop, check the if the value of iterator is Null or not using if condition.
    if(gvn_lst[itetor] == ""):
      # If the given condition is true ,then increment the above initialized count value.
        cou_nt += 1
 # Print the number of Null elements in a given List by printing the above count value.
print("The Number of Null Elements in the above given list = ", cou_nt)

Output:

The Number of Null Elements in the above given list =  2

Method #2: Using For Loop (User input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Take a variable to say ‘count’ and initialize its value with ‘0’
  • Loop from 0 to the length of the above-given list using For Loop.
  • Inside the loop, check if the value of the iterator is Null or not using the if conditional statement.
  • If the given condition is true, then increment the above-initialized count value.
  • Print the number of Null elements in a given List by printing the above count value.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions
# and store it in a variable.
gvn_lst = list( input(
    'Enter some random List Elements separated by spaces = ').split(' '))
# Take a variable say count and initialize it's value with '0'
cou_nt = 0
# Loop from 0 to the length of the above given list using For Loop.
for itetor in range(len(gvn_lst)):
 # Inside the loop, check the if the value of iterator is Null or not using if condition.
    if(gvn_lst[itetor] == ""):
      # If the given condition is true ,then increment the above initialized count value.
        cou_nt += 1
 # Print the number of Null elements in a given List by printing the above count value.
print("The Number of Null Elements in the above given list = ", cou_nt)

Output:

Enter some random List Elements separated by spaces = hello this is btechgeeks 
The Number of Null Elements in the above given list = 1

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.