Skip to content

Tuple Methods

The tuple data type has only two methods. They are count() and index(). Let’s look at each of them in detail. you will learn how to use them with examples.

S.NoMethodDescriptionExample
1count()Returns the number of times a specified value occurs in a tupletuple.count(value)
2index()Searches the tuple for a specified value and returns the position of where it was foundtuple.index(value)
3len()Returns the number of items in a tuplelen(tuple)
4sorted()Returns a new sorted list from elements in the tuplesorted(tuple)
5sum()Sums the items of an iterable from left to right and returns the totalsum(tuple)
6any()Returns True if any element of the tuple is true. If the tuple is empty, returns Falseany(tuple)
7all()Returns True if all elements of the tuple are true. If the tuple is empty, returns Trueall(tuple)
8enumerate()Returns an enumerate object. It contains the index and value of all the items of tuple as a pairenumerate(tuple)
9filter()Constructs iterator from elements of tuple for which function returns truefilter(function, tuple)
10map()Applies function to every item of iterable and returns a list of the resultsmap(function, tuple)

The count() method returns the number of times a specified value appears in the tuple.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(numbers.count(1))
print(numbers.count(2))
print(numbers.count(3))
print(numbers.count(4))
print(numbers.count(5))

Output:

command
C:\Users\username>python tuple_methods.py
4
3
2
1
1

In the example above, we have a tuple of numbers. We use the count() method to count the number of times each number appears in the tuple.

The index() method searches the tuple for a specified value and returns the position of where it was found.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(numbers.index(1))
print(numbers.index(2))
print(numbers.index(3))
print(numbers.index(4))
print(numbers.index(5))

Output:

command
C:\Users\username>python tuple_methods.py
0
1
2
3
4

In the example above, we have a tuple of numbers. We use the index() method to find the position of each number in the tuple.

The len() method returns the number of items in a tuple.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(len(numbers))

Output:

command
C:\Users\username>python tuple_methods.py
11

In the example above, we have a tuple of numbers. We use the len() method to find the number of items in the tuple.

The sorted() method returns a new sorted list from elements in the tuple.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(sorted(numbers))

Output:

command
C:\Users\username>python tuple_methods.py
[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5]

In the example above, we have a tuple of numbers. We use the sorted() method to sort the numbers in the tuple.

The sum() method sums the items of an iterable from left to right and returns the total.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(sum(numbers))

Output:

command
C:\Users\username>python tuple_methods.py
25

In the example above, we have a tuple of numbers. We use the sum() method to sum the numbers in the tuple.

The any() method returns True if any element of the tuple is true. If the tuple is empty, it returns False.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(any(numbers))

Output:

command
C:\Users\username>python tuple_methods.py
True

In the example above, we have a tuple of numbers. We use the any() method to check if any of the numbers in the tuple is true.

The all() method returns True if all elements of the tuple are true. If the tuple is empty, it returns True.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(all(numbers))

Output:

command
C:\Users\username>python tuple_methods.py
True

In the example above, we have a tuple of numbers. We use the all() method to check if all of the numbers in the tuple are true.

The enumerate() method returns an enumerate object. It contains the index and value of all the items of tuple as a pair.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
for index, value in enumerate(numbers):
    print(index, value)

Output:

command
C:\Users\username>python tuple_methods.py
0 1
1 2
2 3
3 4
4 5
5 1
6 2
7 3
8 1
9 2
10 1

In the example above, we have a tuple of numbers. We use the enumerate() method to get the index and value of each number in the tuple.

The filter() method constructs an iterator from elements of tuple for which function returns true.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
def is_even(number):
    return number % 2 == 0
 
even_numbers = tuple(filter(is_even, numbers))
print(even_numbers)

Output:

command
C:\Users\username>python tuple_methods.py
(2, 4, 2, 2)

In the example above, we have a tuple of numbers. We use the filter() method to filter out the even numbers from the tuple.

The map() method applies function to every item of iterable and returns a list of the results.

Here is an example:

tuple_methods.py
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
def square(number):
    return number ** 2
 
squared_numbers = tuple(map(square, numbers))
print(squared_numbers)

Output:

command
C:\Users\username>python tuple_methods.py
(1, 4, 9, 16, 25, 1, 4, 9, 1, 4, 1)

In the example above, we have a tuple of numbers. We use the map() method to square each number in the tuple.

In this tutorial, you learned how to use tuple methods in Python. You also learned how to use each method with examples. Now you can use these methods in your Python programs. You find the examples and other Python tutorials useful, then please share them with others. If you have any questions or feedback, then please drop a note. You can visit official Python documentation for more information on tuple methods. For more tutorials, please visit our Python Central Hub.


diagram a tuple has two methods because the rest would change it mermaid
List and tuple share the same reading operations. Everything a list has that a tuple does not is either a mutator or copy, which a tuple has no need for. So the short method list is not a missing feature -- it is the immutability, visible in the API.
sketch Every method a tuple lacks, and why p5.js
Side by side, the two APIs. A tuple has count and index and nothing else. Each of the nine methods only a list has is highlighted with the reason it cannot exist on a tuple -- eight of them change the sequence, and the ninth is copy, which an immutable object does not need because sharing it is already safe.
pch.quizTag pch.quizDefaultTitle
  1. How many public methods does `tuple` have?

    pch.quizShowAnswer

    B — Two: `count` and `index` — Verified with `dir(tuple)`. Every method a list has beyond those two is either a mutator or `copy` — none of which can apply to an immutable sequence.

  2. Why does `tuple` have no `copy()` method when `list` does?

    pch.quizShowAnswer

    B — A tuple is safe to share, so a copy would serve no purpose — Copying a list protects you from someone else mutating it. Nothing can mutate a tuple, so a copy would be indistinguishable from the original.

  3. What is the cost of `t.index(x)` on a tuple?

    pch.quizShowAnswer

    C — O(n) — it scans — `count` and `index` both scan, on tuples and lists alike. Immutability does not make searching faster — if you look things up repeatedly, use a set or dict.

  4. You need a fixed record of three values used as a dictionary key. Tuple or list?

    pch.quizShowAnswer

    B — Tuple — it is hashable, and the fixed length is part of the meaning — A list is unhashable and cannot be a key at all. The tuple also documents that the length is fixed, which a list does not.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading