Python Central HubSign in

Python Programming / Python Tuple

Tuple Methods

Learn how to use tuple methods in Python. How many methods are available for tuples? What are they? How do you use them? Find out here. We are going to use multiple examples to demonstrate how to use tuple methods in Python.

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.

Table of Methods

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)

count() Method

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

Here is an example:

Python
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:

cmd
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.

index() Method

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

Here is an example:

Python
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:

cmd
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.

len() Method

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

Here is an example:

Python
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(len(numbers))

Output:

cmd
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.

sorted() Method

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

Here is an example:

Python
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(sorted(numbers))

Output:

cmd
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.

sum() Method

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

Here is an example:

Python
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(sum(numbers))

Output:

cmd
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.

any() Method

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:

Python
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(any(numbers))

Output:

cmd
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.

all() Method

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:

Python
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
 
print(all(numbers))

Output:

cmd
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.

enumerate() Method

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:

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

Output:

cmd
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.

filter() Method

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

Here is an example:

Python
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:

cmd
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.

map() Method

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

Here is an example:

Python
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:

cmd
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.

Conclusion

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.

Check yourself

quizCheck yourself
  1. How many public methods does `tuple` have?

    Show answer

    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?

    Show answer

    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?

    Show answer

    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?

    Show answer

    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.

Try it: Tuple Methods Exercises

Exercise 1 – count()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – index()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Convert to List and Back

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did