- How to create a tuple in python
- Different ways to create tuple in Python
- 1. Create an Empty Tuple in Python
- 2. Create a non-empty Tuple in Python
- 4. Create Nested string and List tuple
- 5. Create a list of tuples
- 6. Make list of tuples from dictionary
- How to create an empty tuple in Python?
- Using Braces() to create tuple
- Syntax
- Example
- Output
- Example
- Output
- Using the tuple() function
- Syntax
- Example
- Output
- Example
- Output
- How to Create an Empty Tuple in Python [2 approaches]
- Empty Tuple in Python
- How to create an empty tuple in Python
- 1. Using Empty Parentheses ()
- 2. Using the tuple() Constructor
- Why Would You Need an Empty Tuple in Python?
- Summary
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))
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.
Langtuple = (1, "C#", 3.4,'C++') print('mixed type tuple=',Langtuple)
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)
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)
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 = 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)
type: list of Tuples from dict : [('a', 12), ('b', 15), ('c', 16)]
How to create an empty tuple in Python?
Tuple is one of the data structures of the python programming language. It is used to store multiple values separated by commas in an ordered manner.
It is immutable in the sense that once the tuple is created cannot perform any operations like deleting, appending etc. The elements in the tuple can be int, float, string, binary data types and it allows duplicates of the elements. It uses indexing for accessing the elements. It allows a single element to be in tuple.
Indexing is the concept used for accessing the elements from particular data structures. There are two ways of performing indexing as positive indexing and negative indexing. In positive indexing the index value of the starting element will be 0 and the end element will be length of the tuple where as in negative indexing the index value of the starting element will be -length of tuple and end element will be -1.
There are different ways for creating the empty tuple. Let’s see each way in detail.
Using Braces() to create tuple
The empty tuple can be created using the braces().
Syntax
The following is the syntax.
Example
As you can observe in the following example, when we assign the braces “()” to a variable an empty tuple will be created.
tuple1 = () print("The empty tuple created using brackets: ",tuple1) print(type(tuple1))
Output
The empty tuple created using brackets: ()
Example
In this example we have created an empty tuple using the braces. Since the tuple in Python is immutable, when we try to append values to it, it will generate an error.
tuple1 = () print("The empty tuple created using brackets: ",tuple1) tuple1[0] = 10 print(type(tuple1))
Output
('The empty tuple created using brackets: ', ()) Traceback (most recent call last): File "main.py", line 3, in tuple1[0] = 10 TypeError: 'tuple' object does not support item assignment
Using the tuple() function
The empty tuple can be created using the tuple function available in python.
Syntax
- varibale_name is the name of the tuple
- tuple is the keyword for creating the empty tuple
Example
Here, when we assign the function tuple() to a variable an empty tuple will be created.
tuple1 = tuple() print("The empty tuple created using brackets, ",tuple1) print(type(tuple1))
Output
The empty tuple created using brackets, ()
Example
In this example, we created the tuple using the tuple keyword function and when we try to append an element, it will raise an error as the tuple is immutable.
tuple1 = tuple() print("The empty tuple created using brackets, ",tuple1) tuple1[0] = "Niharika" print(type(tuple1))
Output
The following is the output of the empty tuple created using the tuple keyword.
('The empty tuple created using brackets, ', ()) Traceback (most recent call last): File "main.py", line 3, in tuple1[0] = "Niharika" TypeError: 'tuple' object does not support item assignment
How to Create an Empty Tuple in Python [2 approaches]
Tuples are a very useful data type in Python that allows you to store multiple items in a single variable. Tuples are ordered, immutable, and can contain items of different data types. In this tutorial, we will focus on how to create an empty tuple in Python.
Empty Tuple in Python
An empty tuple in Python is a tuple that does not contain any items. This is useful as a placeholder when you need to create a tuple that will be populated later, or when you need to represent a lack of data in a structured way.
How to create an empty tuple in Python
There are several ways to create an empty tuple in Python:
1. Using Empty Parentheses ()
The simplest and most common way to create an empty tuple in Python is to use an empty pair of parentheses. This is also considered the most Pythonic way.
Here is an example of an empty tuple in Python.
# Create an empty tuple using empty parentheses empty_tuple = () # Print the empty tuple print(empty_tuple) # Check its type print(type(empty_tuple))
You can check the output in the screenshot.
2. Using the tuple() Constructor
You can also create an empty tuple in Python using the tuple() constructor without any arguments.
This approach is slightly less concise, but it makes it clear that you are creating a tuple in Python. This might be preferred in some contexts for clarity.
Here is an example of creating an empty tuple using the tuple() Constructor in Python.
# Create an empty tuple using the tuple() constructor empty_tuple = tuple() # Print the empty tuple print(empty_tuple) # Check its type print(type(empty_tuple))
You can see the screenshot below:
Why Would You Need an Empty Tuple in Python?
You might be wondering why someone would need a Python empty tuple. Here are a few scenarios:
- As a Placeholder: When you need a placeholder for a variable that will hold a tuple later.
- Function with No Arguments: When defining a function that takes no arguments but you still want to use the function call syntax.
- Immutability: Unlike lists, tuples are immutable. So, sometimes it is useful to have an empty immutable collection.
Summary
In this tutorial, we learned two simple ways to create an empty tuple in Python by using either empty parentheses () or the tuple() constructor. Depending on the context, you might choose one over the other for readability and clarity. Empty tuples can be useful in various scenarios such as placeholders, function definitions, and when you need an immutable collection.
You may also like the following tutorials:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.