Skip to content

Tuple Methods

Tuple Methods

The tuple data type has only two methods. They are count()count() and index()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)tuple.count(value)
2index()Searches the tuple for a specified value and returns the position of where it was foundtuple.index(value)tuple.index(value)
3len()Returns the number of items in a tuplelen(tuple)len(tuple)
4sorted()Returns a new sorted list from elements in the tuplesorted(tuple)sorted(tuple)
5sum()Sums the items of an iterable from left to right and returns the totalsum(tuple)sum(tuple)
6any()Returns True if any element of the tuple is true. If the tuple is empty, returns Falseany(tuple)any(tuple)
7all()Returns True if all elements of the tuple are true. If the tuple is empty, returns Trueall(tuple)all(tuple)
8enumerate()Returns an enumerate object. It contains the index and value of all the items of tuple as a pairenumerate(tuple)enumerate(tuple)
9filter()Constructs iterator from elements of tuple for which function returns truefilter(function, tuple)filter(function, tuple)
10map()Applies function to every item of iterable and returns a list of the resultsmap(function, tuple)map(function, tuple)

count() Method

The count()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))
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
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()count() method to count the number of times each number appears in the tuple.

index() Method

The index()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))
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
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()index() method to find the position of each number in the tuple.

len() Method

The len()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))
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
command
C:\Users\username>python tuple_methods.py
11

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

sorted() Method

The sorted()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))
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]
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()sorted() method to sort the numbers in the tuple.

sum() Method

The sum()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))
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
command
C:\Users\username>python tuple_methods.py
25

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

any() Method

The any()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))
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
command
C:\Users\username>python tuple_methods.py
True

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

all() Method

The all()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))
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
command
C:\Users\username>python tuple_methods.py
True

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

enumerate() Method

The enumerate()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)
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
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()enumerate() method to get the index and value of each number in the tuple.

filter() Method

The filter()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)
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)
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()filter() method to filter out the even numbers from the tuple.

map() Method

The map()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)
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)
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()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.

Was this page helpful?

Let us know how we did