89DEVs

Python Tuple count() Method

The Tuple method count() returns the number of times a given element appears in the tuple. This method is useful to count the elements in a tuple.

Tuple count() example

In this example, a tuple of different letters was created. Some of the letters are included multiple times. # tuple count() example letters = ('a', 'a', 'a', 'b', 'c', 'c') print('letter a appears ' + str(letters.count('a')) + ' time(s) in our letters tuple.') print('letter b appears ' + str(letters.count('b')) + ' time(s) in our letters tuple.') print('letter c appears ' + str(letters.count('c')) + ' time(s) in our letters tuple.') A string that includes the count of each element is returned. letter a appears 3 time(s) in our letters tuple. letter b appears 1 time(s) in our letters tuple. letter c appears 2 time(s) in our letters tuple. If the element is not in the tuple an integer value of 0 is returned. # tuple count() example letters = ('a', 'a', 'a', 'b', 'c', 'c') print('letter z appears ' + str(letters.count('z')) + ' time(s) in our letters tuple.') The letter z is not in our tuple, so integer 0 is returned. letter z appears 0 time(s) in our letters tuple.

Count all elements in a tuple

To count all elements in a tuple we can use the count() method within a loop. We loop through all elements and return the count for each element in the tuple. # count all elements in a tuple letters = ('a', 'a', 'a', 'b', 'c', 'c') for letter in letters: print(letter + ' is ' + str(letters.count(letter)) + ' times(s) in our letters tuple.') The loop works, but it repeats some elements because they are included multiple times. a is 3 times(s) in our letters tuple. a is 3 times(s) in our letters tuple. a is 3 times(s) in our letters tuple. b is 1 times(s) in our letters tuple. c is 2 times(s) in our letters tuple. c is 2 times(s) in our letters tuple. To remove duplicates from the loop, we can use the built-in set() function in Python to convert our Tuple into a Set. Sets contain unique values and therefore each element is printed once. # count all unique elements in a tuple letters = ('a', 'a', 'a', 'b', 'c', 'c') for letter in set(letters): print(letter + ' is ' + str(letters.count(letter)) + ' times(s) in our letters tuple.') The count for each element is now returned. a is 3 times(s) in our letters tuple. c is 2 times(s) in our letters tuple. b is 1 times(s) in our letters tuple. The returned elements are now unique, but they are not sorted. We can use the sorted() function to sort them alphabetically. # count all unique elements in a tuple and sort them letters = ('a', 'a', 'a', 'b', 'c', 'c') for letter in sorted(set(letters)): print(letter + ' is ' + str(letters.count(letter)) + ' times(s) in our letters tuple.') Then our elements are returned in alphabetical order. a is 3 times(s) in our letters tuple. b is 1 times(s) in our letters tuple. c is 2 times(s) in our letters tuple.

Tuple count() alternative

An alternative way to count the elements in a tuple is the Collections.Counter(). It returns a Counter of the tuple. # count tuple elements with collections.Counter() from collections import Counter letters = ('a', 'a', 'a', 'b', 'c', 'c') print(Counter(letters)) The Counter is returned. Counter({'a': 3, 'c': 2, 'b': 1}) The advantage of the Counter is that it can give us more information about the tuple. For example the most common element and the least common element. Of course, this can also be achieved with the count() method. # show most common and least common element from collections import Counter letters = ('a', 'a', 'a', 'b', 'c', 'c') counter = Counter(letters) print('the most common element is: ' + str(counter.most_common()[0][0])) print('the least common element is: ' + str(counter.most_common()[:-2:-1][0][0])) The most and least common element is printed. the most common element is: a the least common element is: b

Tuple count() Syntax

The syntax of the Tuple count() method is: tuple.count(element)

Tuple count() arguments

The tuple count() method takes exactly one argument. If more or less than one argument is given, the TypeError exception is raised. The required single argument is the element to count in the tuple.

Tuple count() return value

The tuple count() method returns the count of the given element as an integer. If the element is not included in the tuple, an integer value of 0 is returned.

Other Tuple methods

Tuples have only two methods in Python. The other is the tuple.index() method. It returns the index of a given element in the tuple.

                
        

Summary


Click to jump to section