{"id":16372,"date":"2021-08-12T09:37:38","date_gmt":"2021-08-12T04:07:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=16372"},"modified":"2021-11-22T18:38:25","modified_gmt":"2021-11-22T13:08:25","slug":"python-program-to-count-the-number-of-null-elements-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-null-elements-in-a-list\/","title":{"rendered":"Python Program to Count the Number of Null elements in a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop<\/a> A list can contain \u201cnull elements\u201d in addition to numbers, characters, strings, and so on.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Count the Number of Null elements in a given list.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\n
\nPython lists are similar to arrays in C or Java. A list is a collection of elements.
\nThe 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.<\/p>\nGiven List =[\"btechgeeks\",\"hello\", \"\", 123, \"\" ]<\/pre>\n
The Number of Null Elements in the above given list = 2<\/pre>\n
Given List =[\"\", \"btechgeeks\", \"good \", \"\",\u00a0 \"hello\",\u00a0 \"\",\u00a0 \"\",1,2,9 ]<\/pre>\n
The Number of Null Elements in the above given list = 4<\/pre>\n
Program to Count the Number of Null elements in a List<\/h2>\n
\n
Method #1: Using For Loop (Static input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"btechgeeks\", \"hello\", \"\", 123, \"\"]\r\n# Take a variable say count and initialize it's value with '0'\r\ncou_nt = 0\r\n# Loop from 0 to the length of the above given list using For Loop.\r\nfor itetor in range(len(gvn_lst)):\r\n # Inside the loop, check the if the value of iterator is Null or not using if condition.\r\n if(gvn_lst[itetor] == \"\"):\r\n # If the given condition is true ,then increment the above initialized count value.\r\n cou_nt += 1\r\n # Print the number of Null elements in a given List by printing the above count value.\r\nprint(\"The Number of Null Elements in the above given list = \", cou_nt)\r\n<\/pre>\n
The Number of Null Elements in the above given list = 2<\/pre>\n
Method #2: Using For Loop (User input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions\r\n# and store it in a variable.\r\ngvn_lst = list( input(\r\n 'Enter some random List Elements separated by spaces = ').split(' '))\r\n# Take a variable say count and initialize it's value with '0'\r\ncou_nt = 0\r\n# Loop from 0 to the length of the above given list using For Loop.\r\nfor itetor in range(len(gvn_lst)):\r\n # Inside the loop, check the if the value of iterator is Null or not using if condition.\r\n if(gvn_lst[itetor] == \"\"):\r\n # If the given condition is true ,then increment the above initialized count value.\r\n cou_nt += 1\r\n # Print the number of Null elements in a given List by printing the above count value.\r\nprint(\"The Number of Null Elements in the above given list = \", cou_nt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = hello this is btechgeeks \r\nThe Number of Null Elements in the above given list = 1<\/pre>\n
\n