Code Pumpkin

Tuple In Python

October 28, 2018
Posted by Dipen Adroja

In the last article, we explored the list. In this article, we will talk about tuple. A tuple in python is just like a list of a sequence of immutable python objects. Tuple is similar to list. Same as list it can store different types of objects. The difference between list and tuple is that list is declared in square brackets and can be changed while tuple is declared in parentheses and cannot be changed.

Below are some examples of the tuple:


my_tup = ('Jan’,  2 , ’march')

We can also write empty tuple as below:

my_empty_tup = ();

For writing tuple for a single value, you need to include a comma, even though there is a single value. Also at the end you need to write a semicolon as shown below.

my_single_value_tup = (‘Jan’,);

A tuple can also contain another tuple as an element. The tuple index starts from 0. We can access tuple in the same way as we access list.

Below are some of the example:

my_tup_1 = (1, 2, 3)
my_tup_2 = my_tup_1,(4,5,6)


((1, 2, 3), 4, 5, 6)



Apart from this, we can also have slicing operation, append two tuples to a new tuple and replicate the tuple as shown below:

tup_1 = (1, 2, 3)
tup_2 = (4, 5, 6)

#slicing example of tuple

#Adding two tuple
tup_3 = tup1 + tup2

#tuple replication example
print(tup_1 * 2)


(1, 2)

(1, 2, 3, 4, 5, 6)

(1, 2, 3, 1, 2, 3)

Python provides a tuple assignment feature. Let's check this by an example how it works.

#my tuple with variable values
value_tup = (“Codepumpkin”, ”Python Tuple”, “Pumpkin”)

#assigning these values to variables
(blog_name, article_title, writer) = value_tup




Python Tuple


Tuple also provides some of the built-in functions as shown in below table:




It gives the length of a tuple


It returns the maximum value from the tuple.


It returns the minimum value from a tuple.


It converts the sequence into tuple.


It compares the two Tuples.

Advantages of tuple over the list:

  • Tuples that consist of immutable elements can be used as a key for the dictionary, which is not possible with the list
  • If you have data that is immutable, implementing it as tuple will guarantee that it remains write-protected
  • Iterating through tuple is faster than with list since tuples are immutable.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate â‚ą42 per Tree.

About the Author

Coder, Blogger, Wanderer, Philosopher, Curious pumpkin

Tags: ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook