Tuples | Python Programming Language

Tuples are sequences which are immutable, hold data inside circular brackets and can contain any type of objects. For example – (10, 29, 39) or (82, 29, [19, 37], “ComputerScienceHub”) both of these are tuples.
Below are some examples of Python Tuples.

x = (37, 28, 21, 39)
y = ("Python", "JavaScript", "C#")
z = ("Hello", 29, 46, "How are you?")
# All of these are tuples in Python

3 Ways to create tuples in Python

Creating tuples in Python is super easy, as there exist a number of ways to do this. Below I’ve listed all of those and have also mentioned which way I personally prefer to use.

Creating tuple using circular parenthesis

Tuple can be created by just putting comma separated values inside circular parenthesis. (Super Easy!!) For example – (27, 63, 17) is a tuple in Python.

Creating tuple without using circular parenthesis

Tuples can also be created without circular parenthesis like 37, 98, 34 is a tuple in Python.
But this approach for creating tuples can sometimes be quite confusing. So I would recommend to use Circular Parenthesis Approach rather.

Creating tuple using tuple() Python function

Similar to list() function for creating lists in python, there does exist tuple() function for creating tuples as well.
For example – tuple(19, 63, 82) is a tuple.

Examples showing How to create tuples in Python?

# Creating tuple using circular parenthesis
x = (27,91,45,92,100)
y = (646, 929, 1673)
z = ("I live in Melbourne", "I'm a programmer")

# Creating tuple without using circular parenthesis
a = "Microsoft", "Apple", "Tesla"
b = 18, 65, 82
c = "India", "USA", "UK"

# Creating tuple using tuple() Python function
t = tuple("How are you?", "How's your day")
u = tuple("Samsung", "Xiamo", "SmartPhone")
v = tuple("Business", "Life", "Entrepreneurship")

How to access elements of a tuple in Python?

For accessing element of a tuple just put index of element to be accessed inside square brackets and put it after name of tuple. For example – If we have a tuple x = (19, 28, 28) then to access element at index 1 we can do x[1]. It’s super easy and simple.

Note that you can also use negative values inside square brackets here, in that case Python would start from right to left side of tuple with rightmost value being at index -1.
For example => For tuple x = (74, 28, 82) x[0] = 74 and x[-1] = 82

This method of accessing elements from a tuple can even further be used for accessing elements if tuple is nested(have list/tuple/dicitonary inside it). Like x = (10, [27, 29], { 1: “Hi”, 2: “How”}, “I am programmer”) is a nested tuple.
Below code example shows how to use square brackets for accessing elements inside a nested Python tuple.

x = (10, [27, 29], { 1: "Hi", 2: "How"}, "I am programmer")

x[0]         # Returns 10
x[1]         # Returns [27, 29] a list

x[1][0]      # Returns first element inside list returned by x[1]
x[1][1]      # Returns second element inside list returned by x[1]

x[2]         # Returns dictionary { 1: "Hi", 2: "How"}
x[2][1]      # Returns value of key 1 inside dictionary returned by x[2]
x[2][2]      # Returns value of key 2 inside dictionary returned by x[2]
x[2][3]      # Raises keyError as there is no key 3 in dictionary x[2]

Note 🖐🏻
I would be really careful while accessing element of some element of a nested tuple. As you saw in above example for x[2][3] it raised KeyError. That’s why for dealing with such situations I’ll use if else statements just to make sure element exist before accessing it.

Adding new elements to tuple in Python?

No, you cannot add new elements to a tuple in Python as tuples are immutable by design. Once a tuple is created it can never be changed by adding/removing values from it. If you want to change some existing tuple then you need to create a new one.
For example – If you have a tuple x = (19, 28, 29) and you want to add 10 number to end of this then.

Adding element to a tuple by changing it to a list

  1. Convert tuple first to a list.
    y = list(x)
  2. Append value to end of newly created list
    y.append(10)
  3. Convert list back to tuple by using tuple() python function
    new_tuple = tuple(y)
x = (19, 28, 29)      # A Python tuple
y = list(x)           # Changed tuple to list [19, 28, 29]
y.append(10)          # Added 10 to end of list y [19, 28, 29, 10]
new_tuple = tuple(y)  # Changed list back to tuple

# new_tuple will be (19, 28, 29, 10)

If your unaware of What are lists in Python? Check out this article.

Other stuff related to Python Tuples

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

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

Recent Posts