Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to Add variables to a Tuple

In this article, we will learn to add variables in a tuple in Python. We will use some built-in functions, simple approaches, and some custom code as well. Let's first have a quick look over what is a tuple in Python.

Python Tuple

Python has a built-in data type called a tuple. Data inside a tuple can be of any type say, integer, string or a float value, or even a tuple type. The tuple uses comma-separated values within round brackets or parentheses to store data. Tuples can be defined using any variable name and then assigning different values to the tuple inside the round brackets. The tuple is ordered, unchangeable, and allows duplicate values.

tuple1 = ("Ram", "Arun", "Kiran")
tuple2 = (16, 78, 32, 67)
tuple3 = ("apple", "mango", 16, "cherry", 3.4)

As we know that Tuples are immutable objects in Python. We cannot perform addition, deletion, modification operations on tuples once created. So, in order to add variables or items in a tuple, we have to create a new tuple instead of modifying the original tuple. Let us discuss various ways to add variables or values in a tuple.

Example: Adding Variables to a Tuple

A tuple can be created using Python Variables. Variables are initialized with some values to store in the form of a tuple. Users can even take inputs for the variables. This example shows how you can add existing values and build a tuple. Variable names are stored inside round brackets or parentheses and then the values associated with these variables are printed in the form of a tuple.

name = "John"
age = 40
location = "London"

details = (name, age, location)
print(details)


('John', 40, 'London')

Example: Adding Variables to a Tuple using vars() Function

This example uses an inbuilt vars() function which acts like a locals() function. func() initialized three variables and passed them to another function maketuple(). maketuple() takes variables and their names as arguments. tuple() is used to convert and store the 'n' number of variables in a tuple type. This method is used in complicated cases.

def maketuple(variables, names):
  return tuple(variables[n] for n in names)

def func():
  x = 23
  y = 45
  z = 67
  return maketuple(vars(), 'x y z'.split())
  
print(func())


(23, 45, 67)

Example: Convert Tuple to a List to Add Items in a Tuple

This method adds items to an already created tuple. It simply converts the original tuple into a list and adds items using append() function of the list. It then converts the new lists back to the tuple. This method is generally used when the user has to pass a tuple as a function argument, which is often necessary for the NumPy functions.

input_tuple = ("apple", "banana", "mango")

#converts to list
list1 = list(input_tuple)

#adds new item to the list
list1.append("strawberry")

#converts list back to tuple
input_tuple = tuple(list1)

print(input_tuple)


('apple', 'banana', 'mango', 'strawberry')

Example: Add Items to a Tuple by Defining a New Tuple

We cannot change an existing tuple but can create a new tuple and concatenate the old tuple using + operator. If you want to add a single element, make it a singleton like (3,). You can add a tuple of multiple elements with or without that trailing comma. The trailing comma is necessary for the singleton to avoid confusion between an element in parentheses.

tuple1 = (1, 2, 3)
tuple2 = tuple1 + (4, 5, 6)
print(tuple2)

print("Adding to an empty tuple:")
t1 = ()
t2 = t1 + (1,2)
print(t2)
t3 = t2 + (3,)
print(t3)


(1, 2, 3, 4, 5, 6)
Adding to an empty tuple:
(1, 2)
(1, 2, 3)

Note: If you do not add the trailing comma, you will get the following error.

TypeError: can only concatenate tuple (not "int") to tuple.

Conclusion

In this article, we learned to add variables and values to a tuple in Python by using several methods. We used some simple algorithms like concatenation, defining a new tuple, converting tuple to a list, and using the vars() function to add existing values to a tuple. We discussed that all these methods cannot change the existing tuple instead created a new tuple.