Javatpoint Logo
Javatpoint Logo

Python Tuples

A collection of ordered and immutable objects is known as a tuple. Tuples and lists are similar as they both are sequences. Though, tuples and lists are different because we cannot modify tuples, although we can modify lists after creating them, and also because we use parentheses to create tuples while we use square brackets to create lists.

Placing different values separated by commas and enclosed in parentheses forms a tuple. For instance,

Example

We represent an empty tuple by two parentheses enclosing nothing.

We need to add a comma after the element to create a tuple of a single element.

Tuple indices begin at 0, and similar to strings, we can slice them, concatenate them, and perform other operations.

Creating a Tuple

All the objects (elements) must be enclosed in parenthesis (), each separated by a comma, to form a tuple. Although using parenthesis is not required, it is recommended to do so.

Whatever the number of objects, even of various data types, can be included in a tuple (dictionary, string, float, list, etc.).

Code

Output:

Empty tuple:  ()
Tuple with integers:  (4, 6, 8, 10, 12, 14)
Tuple with different data types:  (4, 'Python', 9.3)
A nested tuple:  ('Python', {4: 5, 6: 2, 8: 2}, (5, 3, 5, 6))

Parentheses are not mandated to build tuples. Tuple packing is the term for this.

Code

Output:

(4, 5.7, 'Tuples', ['Python', 'Tuples'])
<class 'tuple'>
<class 'TypeError'>

It can be challenging to build a tuple with just one element.

Placing just the element in parentheses is not sufficient. It will require a comma after the element to be recognized as a tuple.

Code

Output:

<class 'str'>
<class 'tuple'>
<class 'tuple'>

Accessing Tuple Elements

We can access the objects of a tuple in a variety of ways.

Indexing

To access an object of a tuple, we can use the index operator [], where indexing in the tuple starts from 0.

A tuple with 5 items will have indices ranging from 0 to 4. An IndexError will be raised if we try to access an index from the tuple that is outside the range of the tuple index. In this case, an index above 4 will be out of range.

We cannot give an index of a floating data type or other kinds because the index in Python must be an integer. TypeError will appear as a result if we give a floating index.

The example below illustrates how indexing is performed in nested tuples to access elements.

Code

Output:

Python
Tuple
tuple index out of range
tuple indices must be integers or slices, not float
l
6

Negative Indexing

Python's sequence objects support negative indexing.

The last item of the collection is represented by -1, the second last item by -2, and so on.

Code

Output:

Element at -1 index:  Collection
Elements between -4 and -1 are:  ('Python', 'Tuple', 'Ordered')

Slicing

We can use a slicing operator, a colon (:), to access a range of tuple elements.

Code

Output:

Elements between indices 1 and 3:  ('Tuple', 'Ordered')
Elements between indices 0 and -4:  ('Python', 'Tuple')
Entire tuple:  ('Python', 'Tuple', 'Ordered', 'Immutable', 'Collection', 'Objects')

Deleting a Tuple

The elements of a tuple cannot be changed, as was already said. Therefore, we are unable to eliminate or remove elements of a tuple.

However, the keyword del makes it feasible to delete a tuple completely.

Code

Output:

'tuple' object doesn't support item deletion
name 'tuple_' is not defined

Repetition Tuples in Python

Code

Output:

Original tuple is:  ('Python', 'Tuples')
New tuple is:  ('Python', 'Tuples', 'Python', 'Tuples', 'Python', 'Tuples')

Tuple Methods

Tuple does not provide methods to add or delete elements, and there are only the following two choices.

Examples of these methods are given below.

Code

Output:

2
2

Tuple Membership Test

Using the in keyword, we can determine whether an item is present in the given tuple or not.

Code

Output:

True
False
False
True

Iterating Through a Tuple

We can use a for loop to iterate through each element of a tuple.

Code

Output:

Python
Tuple
Ordered
Immutable

Changing a Tuple

Tuples, as opposed to lists, are immutable objects.

This implies that after a tuple's elements have been specified, we cannot modify them. However, we can modify the nested elements of an element if the element itself is a mutable data type like a list.

A tuple can be assigned to many values (reassignment).

Code

Output:

'tuple' object does not support item assignment
('Python', 'Tuple', 'Ordered', 'Immutable', [1, 2, 10, 4])
('Python', 'Items')

To merge multiple tuples, we can use the + operator. Concatenation is the term for this.

Using the * operator, we may also repeat a tuple's elements for a specified number of times. This is already shown above.

The results of the operations + and * are new tuples.

Code

Output:

('Python', 'Tuple', 'Ordered', 'Immutable', 4, 5, 6)

Advantages of Tuple over List

Tuples and lists are employed in similar contexts because of how similar they are. A tuple implementation has several benefits over a list, though. The following are a few of the primary benefits:

  • We generally employ lists for homogeneous data types and tuples for heterogeneous data types.
  • Tuple iteration is quicker than list iteration because tuples are immutable. There is such a modest performance improvement.
  • Tuples with immutable components can function as the key for a Python dictionary object. This feature is not feasible with lists.
  • Collecting data in a tuple will ensure that it stays write-protected if it never changes.

Next TopicPython Set





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA