{"id":24717,"date":"2021-11-02T09:45:54","date_gmt":"2021-11-02T04:15:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=24717"},"modified":"2021-11-05T17:23:15","modified_gmt":"2021-11-05T11:53:15","slug":"python-list-count-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-count-method-with-examples\/","title":{"rendered":"Python List count() method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Tuple index() method with Examples<\/a> Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n \u00a0List count() method in Python:<\/strong><\/p>\n The count() function returns the number of occurrences of the particular element in the given list.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n The count() method only accepts one argument.<\/p>\n Return Value:<\/strong><\/p>\n The count() method returns the number of occurrences of the given element in the given List.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Example-1:<\/strong><\/p>\n String List:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example-2:<\/strong><\/p>\n Integer List:<\/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 Covered all examples on python concepts and missed checking out the Python List Method Examples<\/a> then go with the available link & gain more knowledge about List method concepts in python.<\/p>\n In the previous article, we have discussed Python Tuple index() method with Examples List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of …<\/p>\n
\nList in Python:<\/strong><\/p>\ngivenlist.count(gvnelement)<\/pre>\n
\n
Given List = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']\r\nGiven Element = \"python\"<\/pre>\n
The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :\r\n2<\/pre>\n
Given List = [7, 1, 8, 2, 8, 1, 9, 8, 4]\r\nGiven Element = 8<\/pre>\n
Enter some random list elements = 7 1 8 2 8 1 9 8 4\r\nEnter some random element = 8\r\nThe count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :\r\n3<\/pre>\n
List count() method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string List as static input and store it in a variable.\r\ngvnlst = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']\r\n# Give the element whose frequency is to be calculated as static input\r\n# and store it in another variable.\r\ngvnelemeent = \"python\"\r\n# Pass the given element above as the argument to the count function\r\n# for the given List and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given List).\r\nresltcnt = gvnlst.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given List', gvnlst, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :\r\n2<\/pre>\n
\n
# Give the Integer List as static input and store it in a variable.\r\ngvnlst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5]\r\n# Give the element whose frequency is to be calculated as static input\r\n# and store it in another variable.\r\ngvnelemeent = 3\r\n# Pass the given element above as the argument to the count function\r\n# for the given List and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given List).\r\nresltcnt = gvnlst.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given List', gvnlst, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
The count of the given element { 3 } in the given List [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5] is :\r\n4<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the Integer List as user input using list(),map(),input(),and split() functions.\r\ngvnlistt = list(map(int,input('Enter some random list elements = ').split()))\r\n# Give the element as user input using the int(), input() functions,\r\n# and store it in another variable.\r\ngvnelemeent = int(input('Enter some random element = '))\r\n# Pass the given element above as the argument to the count function\r\n# for the given list and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given list).\r\nresltcnt = gvnlistt.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given list', gvnlistt, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
Enter some random list elements = 7 1 8 2 8 1 9 8 4\r\nEnter some random element = 8\r\nThe count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :\r\n3<\/pre>\n
\n