Python Tuples – Creating, Updating, Accessing and Deleting Elements

In this article, we will learn about what are tuples in python, how to create and update a tuple. Further we will also see how to access and delete various items in the tuple.

What are Tuples?

Tuples in python are very similar to lists. Tuples are also sequence type of and supports heterogeneous data in them.

A tuple in python can be defined as a collection of multiple elements of different or same data type.

What are the Differences Between List and Tuple?

How ever there are some differences between tuples and lists.

  • Lists in python are mutable, while tuples are immutable.

Mutableit means once you create a data structure (list in this case), it can be modified. That means new items can be inserted, or existing items can be deleted, item values can be updated etc.

Immutableit means, after creation of data structure (tuple in this case), it is not possible to edit or modify the tuple. That means new items cannot be inserted or existing items cannot be deleted directly from the tuple.

  • Elements in the list are separated by commas and are represented inside square brackets i.e., [ ]. In case of tuple, elements are separated by comma but are represented inside of a parenthesis i.e, ( ).

Example:-

List1 = ['apple', 24, 35.67, True, "Rama"]
Tuple1 = ('apple', 24, 35.67, "Rama")
  • Methods such as append( ), insert( ), extend( ) which are used for updating new elements into list cannot be used for tuples. Because tuples cannot be edited after they are created. How ever, it is possible to assign elements in a tuple with few more elements into another tuple. This allows the original tuple to remain same or constant.
  • Methods such as del( ), remove( ), pop( ) do not work with tuple.

How to Create a Tuple in Python?

A tuple in python can be created in different ways and in different styles depending on our requirement. However, the basic syntax for creating a tuple is as follows:

variable_or_tuple_name = ( Elements_in_the_tuple )

Examples for Creating Different Types of Tuples:

Tuple with 4 elements:


tuple1 = (12, 15, 32, 54) #tuple with 4 elements

As tuples are sequences similar to lists, for elements in tuples indexing is present. Indexing starts from 0. That means 0, 1, 2, 3 are the index for 12, 15, 32 and 54 respectively in tuple1 given above.

Creating an Empty Tuple:

tupleEmp = ( )  #This line of code creates an empty tuple 

Tuple with single element:

#Mistake_Alert
tupSing = (1) ## this is not a tuple

The above way of assignment doesn’t create a tuple. Instead it creates an integer variable named tupSing and assigns value 1 into it.

To store a single element into a tuple, we have to place a comma symbol next to the element. This comma helps interpreter in knowing tupSing is a tuple type.

#Correct Syntax for Single Element tuple
tupSing = (1,) ##correct syntax

Creating tuple without parenthesis:
Tuple can also be created without parenthesis by using comma symbols as shown below.

tupNoParen = 8, 11, 9, 25, 98  # this is a tuple with 5 elements created
without parenthesis

How to Update the elements in a Tuple?

As we already know, tuple is immutable. That means we cant add elements into an already existing tuple. But it is possible to take elements from different tuples and create or update a new tuple. This is also called as concatenation of multiple tuples. It is performed as shown below.

tup1 = (1, 2, 3, 4, 5)            # tup1 is tuple 1 containing integers

tup2 = ("ram", "raja", "kiran", "Lata")    #tup2 is tuple2 containing some strings 

tup3 = tup1+tup2    # by combining tup1 and tup2 a new tuple tup3 is created here.

We can update the tup1 or tup2 directly. Hence, by concatenating tuples as shown above is a technique for updating tuples. There exists another way, where we can convert a tuple into a list, then update or modify it with required details, then convert back it into a tuple. This method is not discussed in the present context of updating tuple.

How to Remove or Delete the elements in a Tuple?

The individual elements of a tuple cannot be deleted or removed from it. If we try to delete a selected element from tuple, the compiler will return an error. However, it is possible to delete entire tuple.

tup1 = (10, 25, 37, 58, 99)

del tup1[1]  # doesn't execute, returns error

del tup1  #entire tuple tup1 is deleted

print(tup1)  #this statement doesn't execute as tup1 is already deleted and doesn't exist.

How to Access the elements in a Tuple or Slicing the Tuple?

All elements in a tuple have an index for them. Index for elements in the tuple starts with 0. Elements of a tuple are accessed with index or through index slicing process.

SlicingIt is a process where a specific portion of a sequence is selected by specifying index range.

For example, in the above tup1 = (10, 25, 37, 58, 99) elements are accessed using index as shown below.

tup1[0] # for accessing the 1st element in tuple. 

tup1[3] #for accessing the 4 th element in tuple.

tup1[0] and tup1[3] are equal to 10 and 58 as per the index in the tuple tup1.

Slicing is performed using a colon operator present in square brackets i.e., either [ : ] or [ : : ].

Different use cases of slicing on tuples:

Access first few elements of tuple:

tup1[0:3] #here 0 is starting index and upto 3rd item i.e., index 2 items are retrieved from tuple

>>>output will be : 10, 25, 37 

tup1[ :3] # Default start index will be 0 in case if not mentioned in slicing, here also first 3 items are retrieved from tuple.

>>> output will be : 10, 25, 37

Access a specific portion or last few elements of tuple:

tup1[2:4] #this collects from 3rd to 4th

>>> output will be: 37, 58

tup1[2: ] # this collects from 3rd to last element of tuple

>>> output will be: 37, 58, 99

 

To summarize, in this tutorial we have learned about how to create a tuple, update and access elements of a tuple and delete a tuple.

If you have any queries, post them in the comment section below.

Thank you.

Add a Comment

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