Accessing Elements – tuples¶
Let us see details related to operations on tuples. Unlike other collections (list
, set
, dict
) we have limited functions with tuple
in Python.* tuple
is by definition immutable and hence we will not be able to add elements to a tuple or delete elements from a tuple.
- Only functions that are available are
count
andindex
. count
gives number of times an element is repeated in a tuple.index
returns the position of element in a tuple.index
can take up to 3 arguments –element
,start
andstop
.
In [1]:
t = (1, 2, 3, 4, 4, 6, 1, 2, 3)
In [2]:
help(t)
Help on tuple object: class tuple(object) | tuple(iterable=(), /) | | Built-in immutable sequence. | | If no argument is given, the constructor returns an empty tuple. | If iterable is specified the tuple is initialized from iterable's items. | | If the argument is a tuple, the return value is the same object. | | Built-in subclasses: | asyncgen_hooks | UnraisableHookArgs | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(self, key, /) | Return self[key]. | | __getnewargs__(self, /) | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value. | | __ne__(self, value, /) | Return self!=value. | | __repr__(self, /) | Return repr(self). | | __rmul__(self, value, /) | Return value*self. | | count(self, value, /) | Return number of occurrences of value. | | index(self, value, start=0, stop=9223372036854775807, /) | Return first index of value. | | Raises ValueError if the value is not present. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature.
In [3]:
t.count?
Signature: t.count(value, /) Docstring: Return number of occurrences of value. Type: builtin_function_or_method
In [4]:
t.count(4)
Out[4]:
2
In [5]:
t.count(9)
Out[5]:
0
In [6]:
t.index?
Signature: t.index(value, start=0, stop=9223372036854775807, /) Docstring: Return first index of value. Raises ValueError if the value is not present. Type: builtin_function_or_method
In [7]:
t.index(2) # Scans all the elements
Out[7]:
1
In [8]:
t.index(2, 3) # Scans all the elements starting from 4th
Out[8]:
7
In [9]:
t.index(2, 3, 5) # throws ValueError, scans from 4th element till 5th element
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [9], in <cell line: 1>() ----> 1 t.index(2, 3, 5) ValueError: tuple.index(x): x not in tuple
In [10]:
t.index(9)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 t.index(9) ValueError: tuple.index(x): x not in tuple
In [11]:
t.index(6, 3, 5) # throws ValueError, scans from 4th element till 5th element
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [11], in <cell line: 1>() ----> 1 t.index(6, 3, 5) ValueError: tuple.index(x): x not in tuple
In [12]:
t.index(6, 3, 6)
Out[12]:
5