C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python List count() MethodPython count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0. The examples and syntax is described below. Signaturecount(x) Parametersx: element to be counted. ReturnIt returns number of times x occurred in the list. Let's see some examples of count() method to understand it's functionality. Python List count() Method Example 1It is a simple example to understand how a count method works. # Python list count() Method # Creating a list apple = ['a','p','p','l','e'] # Method calling count = apple.count('p') # Displaying result print("count of p :",count) Output: count of p : 2 Python List count() Method Example 2If the passed value is not present in the list, the method returns 0. See the example below. # Python list count() Method # Creating a list apple = ['a','p','p','l','e'] # Method calling count = apple.count('b') # Displaying result print("count of b :",count) Output: count of b : 0 Python List count() Method Example 3Python count() method can also be used to check whether an element is duplicate in the list or not. The below example checks the duplicacy of the element in the list. # Python list count() Method # Creating a list apple = ['a','p','p','l','e'] # Method calling count = apple.count('p') # Displaying result if count>=2: print("value is duplicate") print("count of p :",count) Output: value is duplicate count of p : 2
Next TopicPython Lists
|