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.No | Method | Description | Example |
---|---|---|---|
1 | count() | Returns the number of times a specified value occurs in a tuple | tuple.count(value) tuple.count(value) |
2 | index() | Searches the tuple for a specified value and returns the position of where it was found | tuple.index(value) tuple.index(value) |
3 | len() | Returns the number of items in a tuple | len(tuple) len(tuple) |
4 | sorted() | Returns a new sorted list from elements in the tuple | sorted(tuple) sorted(tuple) |
5 | sum() | Sums the items of an iterable from left to right and returns the total | sum(tuple) sum(tuple) |
6 | any() | Returns True if any element of the tuple is true. If the tuple is empty, returns False | any(tuple) any(tuple) |
7 | all() | Returns True if all elements of the tuple are true. If the tuple is empty, returns True | all(tuple) all(tuple) |
8 | enumerate() | Returns an enumerate object. It contains the index and value of all the items of tuple as a pair | enumerate(tuple) enumerate(tuple) |
9 | filter() | Constructs iterator from elements of tuple for which function returns true | filter(function, tuple) filter(function, tuple) |
10 | map() | Applies function to every item of iterable and returns a list of the results | map(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:
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))
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:
C:\Users\username>python tuple_methods.py
4
3
2
1
1
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:
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))
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:
C:\Users\username>python tuple_methods.py
0
1
2
3
4
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(len(numbers))
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(len(numbers))
Output:
C:\Users\username>python tuple_methods.py
11
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(sorted(numbers))
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(sorted(numbers))
Output:
C:\Users\username>python tuple_methods.py
[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5]
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(sum(numbers))
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(sum(numbers))
Output:
C:\Users\username>python tuple_methods.py
25
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(any(numbers))
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(any(numbers))
Output:
C:\Users\username>python tuple_methods.py
True
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(all(numbers))
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
print(all(numbers))
Output:
C:\Users\username>python tuple_methods.py
True
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:
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
for index, value in enumerate(numbers):
print(index, value)
numbers = (1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1)
for index, value in enumerate(numbers):
print(index, value)
Output:
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
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:
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)
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:
C:\Users\username>python tuple_methods.py
(2, 4, 2, 2)
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:
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)
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:
C:\Users\username>python tuple_methods.py
(1, 4, 9, 16, 25, 1, 4, 9, 1, 4, 1)
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