How to create a tuple in python

Today we are going how to create a tuple in python. A tuple is a collection of items that are ordered and unchangeable. Tuples are created by using a round bracket(). Tuples are faster than lists in Python because tuples are immutable which means the items can not be changed once they are assigned, but we can change the items of a list. In this post, we are going to understand easy ways how to create/make a tuple in python?. Let us understand how to create tuples in Python.

Different ways to create tuple in Python

1. Create an Empty Tuple in Python


First, we will see how to create an Empty tuple by using round brackets which are used as a placeholder. In this python program, we are creating an empty tuple we are using the () parenthesis and verifying the type of created tuple by using the python type() method.

Langtuple = ()
print('The empty tuple=',Langtuple)
print(type(Langtuple))

Output

The empty tuple= ()
<class 'tuple'>

2. Create a non-empty Tuple in Python


Tuples in python is create by using () parenthesis. In this example we are creating a non empty Tuple with three items of tuple of string data types.

#non-empty tuple

langtuple = ('tuple', 'python','C#')
print('one way to define non_empty tuple =\n',langtuple)

Output

one way to define non_empty tuple =
 ('tuple', 'python', 'C#')

3. Create Tuple of mixed datatypes


Tuples in Python can have data of any datatype it can be int, string, float. Here we are creating tuple with mixed data types includes int, string, float.

Langtuple = (1, "C#", 3.4,'C++')
print('mixed type tuple=',Langtuple)

Output

mixed type tuple= (1, 'C#', 3.4, 'C++')

4. Create Nested string and List tuple


We can create a nested tuple in python, In this example, we are creating a tuple that contains string items tuple(langtuple), int items tuple (tupint), and list(mylist) as items of the tuple. We are passing all three( langtuple,tupint,mylist) to () parathesis to separated by comma(,) to create a nested tuple.

#defining a non-empty tuple
langtuple = ('non-empty','tuple','in','python')

mylist = [1, 2, 3]
tupint = (8, 4, 6)

#creating a nested tuple

nested_tuple = (langtuple, tupint, tuple(mylist))

print('nested tuple in python = \n',nested_tuple)

Output

nested tuple in python = 
 (('non-empty', 'tuple', 'in', 'python'), (8, 4, 6), [1, 2, 3])

5. Create a list of tuples


Sometimes we need to create a tuples list of tuples, To create a list of tuples in the below example we are using the zip() method and passing the lists(strlist,numlist) to create a list of tuples.

strlist = ['non-empty','tuple','in','python']
numlist =[12,14,15,16]

list_of_tuples = list(zip(strlist,numlist))

print('list of tuples: \n',list_of_tuples)

Output

list of tuples: 
 [('non-empty', 12), ('tuple', 14), ('in', 15), ('python', 16)]

6. Make list of tuples from dictionary


We can create/make a tuple from dictionary collection of python.In this blow code example we are going to understand how to achieve this.

mydict = {'a':12,'b':15,'c':16}

dict_to_list_of_tups = list(dict.items(mydict))
  

print('type:',type(dict_to_list_of_tups ))

print('list of Tuples from dict :\n',dict_to_list_of_tups)

Output

type: <class 'list'>
list of Tuples from dict :
 [('a', 12), ('b', 15), ('c', 16)]