Tuples in Python

Tuples in Python

Tuples in Python are the types in which the user cannot change the elements once they are inserted or assigned. They are just similar to lists but we can change the elements of the list once assigned. A tuple can be created in python by putting the elements inside β€˜( )’ these brackets. There is no boundary for the elements in a tuple.  It can have a number of elements as a user wants. They can be of different data types as well such as int, float, char etc.

In case we want to put a single element inside a tuple, the user can put a comma after the single element. Otherwise, the program doesn’t consider that element to be in a tuple. For example, it can be written as (100, ).

Different ways to access the elements of a Tuple in Python:

Indexing: Just like an array, the index of a tuple starts from 0. The user can use β€˜[ ]’ operator to find the index of an element. For example, if a tuple has 8 elements, the index will be from 0 to 7. This user gets an IndexError exception in case he tries to access the elements outside of the tuple index. There can be another error as well which is TypeError. It occurs when the index is not an integer. Hence, the index has to be integer always in a tuple.

Negative Indexing: The index in python can be negative as well. it process in a way such that -1 is the last item of the index, -2 is the second last, -3 is the third last as so on in the tuple. Check out the python code below to understand indexing and negative indexing in tuples:

dh_tuple = ('a','b','c','d','e','f','g','h','i')

The output of this program would be:

('b', 'c', 'd')
('a', 'b')
('h', 'i')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')

Slicing:  If a user wants to access a particular range of the elements of a tuple, he can use slicing. It is done using β€˜:’ operator called the colon. It slices the tuple to take out a range that the user wants to work on.

Python program to understand the use of tuples using different data types:

dh_tuple = ()
dh_tuple = (10, 20, 30)
dh_tuple = (100, "Welcome", 10.6)
dh_tuple = ("Developer Helps", [2, 0, 9], (71, 20, 34))

The output of this python program would be:

(10, 20, 30)
(100, 'Welcome', 10.6)
('Developer Helps', [2, 0, 9], (71, 20, 34))

Advantages of Tuples over Lists:

As we know that tuples are very much similar to the lists. We prefer lists of tuples where we do not wish to change the data types in future. As tuples can have multiple data types, they are highly used.

  • As elements cannot be changed of a tuple, iteration through them is much faster as compared to the lists. Hence, performance in a tuple is much better.
  • Tuples also play a major role in dictionary as they are non mutable.
  • The work done using tuple is always write-protected. This property is because of the fact that elements in a tuple cannot be changed.
  • Elements in a tuple can have different types of data types in a similar code, whereas it is not possible in case of lists.

Tuples V/S Lists

In Python, tuples and lists are both used to store collections of items, but they have some key differences. Here are some unique differences between tuples and lists:

CriteriaPython TuplesPython Lists
MutabilityTuples are Immutable, meaning once created, you cannot change their elements. Any attempt to modify a tuple will result in an error.Lists are mutable, meaning you can change their elements after they are created. You can add, remove, or modify elements in a list.
SyntaxTuples are defined using parentheses() or just commas.Lists are defined using square brackets [].
UsageTuples are typically used when you want to create a collection of values that should not be changed.Β·         
Lists are commonly used when you need a collection of elements that can be modified.
RepresentationTuples are often used to represent fixed sets of related values, such as coordinates or database records.Lists are store and manipulate data dynamically.
PerformanceTuples are generally more memory-efficient and faster to access compared to lists because of their immutability. Since tuples cannot be modified, Python can optimize their memory usage.Lists require additional memory allocation to accommodate potential modifications.
Use as Dictionary KeysTuples can be used as keys in dictionaries.Β·Lists can not be used as keys in dictionaries.
Common operationsTuples lack these operations like adding, removing or updating due to their immutability. 
Β·Since lists are mutable, you can perform operations such as appending, extending, inserting, and deleting elements.
ExampleTuple can be defined as (1, 2, 3).List can be defined as [1, 2, 3].
Python Tuples vs Python Lists

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!