Pages

Sunday, January 3, 2021

Working with Tuples in Python

Tuple

Like an array, Tuple is a collection of data (of different data types). Tuples are specified using round brackets. 

Unlike Lists, Data in the tuple cannot be altered once a tuple is created. Below are some of the features of Tuples.  
  • Tuples can hold data of different data types. 
  • Tuples can not be amended.
  • Data in the tuple can be accessed by using index. Index of an element will remain same once defined.
  • Tuples can hold duplicate data. 

In this post, we will see how to access the data in Tuples and couple of tuple methods.

Creating a Tuple in Python

Like any other data type in Python, Tuple can be created by defining the data inside round brackets. 

Creating tuple in Python

Tuple can be created with data of different data types (or same type). 

In the above example,
  • 'a_tuple' is created with numeric data.
  • 'b_tuple' is created with character data.
  • 'c_tuple' is created with boolean data. 
  • 'd_tuple' is created with data of different data types.
All the tuples defined above has more than one element. Let's see how to define a tuple with just one element.  

Should it just be mentioning one element within round brackets? No, Just mentioning one element with in round brackets would consider the variable as the type of the data mentioned within round brackets. 

This can be easily understood with below example. 

Create tuple with just one element

In the above example, 
  • 'a_tuple' is defined just by mentioning a string with in round brackets. This wouldn't be considered as a tuple instead variable 'a_tuple' would be defined as a string.
  • 'b_tuple' is defined by mentioning comma (',') after the first element. This would define the variable as a tuple. 
print type of tuple

Accessing the data in a Tuple

Elements in a tuple can be accessed using index. Index starts with '0' and incremented by '1'.

Index for a tuple

Elements of a Tuple can be accessed from the last element with the negative index value. As we can see above, Index of last element would be '-1'. 

If we need to retrieve the second element ("TWO"), index '1' or '-4' needs to be mentioned within square brackets. 

E.g.: 

print(a_tuple[1]) #prints the second element of a list

print(a_tuple[-4]) #prints the fourth element from end of a list

In the above example, index is mentioned with in square brackets. Yes, Index needs to be mentioned with in square brackets for a tuple (not in round brackets like it is defined). 

Retrieve Index of an element

We have seen how to access the elements using an index, If we know the element and need to know the index, Method 'index()' would be helpful. One thing to note is, it always returns the first index if the element is present multiple times in the list. 

Retrieve index of an element in a tuple

In the above example, '2' is present twice in the list. But, only one index (first occurrence '1') will be returned.

Print list in Python

By using the index() method we would know the first occurrence of an element. Let's say if there are duplicate entries like in above example, we would need to pass the starting index along with the value. 

Retrieve index of an element in Tuple

In the above example (Line - 4), 'index(2, 2)' indicates to check for the value '2' (first argument) from the index '2' (second argument) and returns the index.


Count the number of occurrences

We have seen how to retrieve the index of an element. Let's say we are only interested in knowing how many times a value is present in the list (and not interested in the index), method 'count()' is helpful. 

Method 'count()' accepts the value to be checked as an arguments and returns the number of occurrences the data is present in a tuple. 

Count the number of occurrences in a Tuple

In the above example, '2' is present '3' times. So, count() should return '3'. 

print tuple

Hope the above details were a bit of help to you in understanding more about Tuples in Python.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

No comments:

Post a Comment

Exception Handling in Python (try - except-else-finally)

Exception Handling in Python Exception handling is key for any programming language. When an exception occurs Python interpreter stops execu...