Python tuple; Through this tutorial, i am going to show you what is Python tuple and how to create tuple in python. As well as, how to add or remove elements from python tuple and so on.
Python Tuple With Example
Simultaneously tell you. Which type of in-build python methods use with python tuple. In-build methods are also included with the example. Which will make you easier to understand.
What is Tuple in python?
The tuple in python are collections of items. which is ordered and unchangeable. The lists are defined by having values between round brackets ().
How to create a tuple in python?
To create a tuple in Python, place all the items within () brackets and separate the items with commas.
A Python tuple can contain elements of different data types. Such as integer, float, string, etc.
python create an empty tuple
Create an empty list in python:
# empty tuple my_tuple = ()
python create tuple with numbers
Create a tuple in Python, in which tuple will be Numbers:
# tuple of integers my_tuple = (1, 2, 3)
python create a tuple with string numbers
Create another list which will contain numbers, strings, etc:
# tuple with mixed datatypes my_list = (1, "Hello", 3.4)
How to access elements from a python tuple?
Create tuple in Python with different data types. and how to access items / elements from tuple in python.
python access- tuple element by index
# tuple of integers my_tuple = (10, 20, 30) print(my_tuple)
As there is a tuple list, from this tuple list have access to the items according to the index.
access tuple in list python using negative index
Negative indexing means beginning from the end,
-1 refers to the last item,
-2 refers to the second last item etc.
# tuple list of integers my_tuple = (10, 20, 30) print(my_tuple[-1])
python list get element by index range
You can get items in the range from the list. For this, the start and end range values have to be passed:
# tuple list of integers my_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90) print(my_tuple[2:6])
(30, 40, 50, 60)
Find length of tuple python
If you want to find/get the tuple list’s length, then you have to use the Python len() function.
You can see the below example how the tuple list is find by length:
# tuple list of integers my_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90) print(len(my_tuple))
Add an element to tuple python
If you have created a tuple list in Python once. You cannot add items to it. You may have found that tuple is unchangeable in the given definition.
Remove item from tuple python
By now you have learned how to create a tuple list in python. Now you will learn how to delete/remove items from the Python tuple list.
If you want to remove all items from the Python tuple list, you can also use del keyword for this
Let’s take an example to understand better how to use the del keyword and remove all the item from the list:
# tuple list of integers my_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90) del my_tuple print(my_tuple)
Removed all item from tuple list and it will this will raise an error because the tuple no longer exists
NameError: name 'my_tuple' is not defined
join tuple python
If you want to join two tuple lists in Python. So you can use its + operator. This will join your two tuple lists in python.
Now take an example. In this example, two tuple lists are taken, one is a list of string data types and the other is a list of integer data types. And will use the + operator to connect to it:
list1 = ("a", "b" , "c") list2 = (1, 2, 3) list3 = list1 + list2 print(list3)
('a', 'b', 'c', 1, 2, 3)
python convert list to tuple
If you have a list in Python. And you want to convert that list to Tuple. So you can do it.
Let’s understand this with an example:
First, let us make a list. Which will contain integer numbers:
list1 = [0, 1, 2]
Now will change this list to the tuple list by using the tuple() function.
list1 = [0, 1, 2] print(tuple(list1))
(0, 1, 2)
Passed our python list inside the tuple () function. And Tuple () function converted to Tuple.
Advantages of Python Tuple over List
Since Python tuples are quite similar to lists, both are also used in similar situations.
However, implementing a python tuple in a list has some advantages. The main benefits listed below are:
- Python tuple can be used with different data types and similar data types.
- If Python Tuple iterates through the loop, it is faster than the list. So there is a slight performance boost.
- If you have data that you do not want to change, you can use tuple. It is guaranteed that it is write-protected.
- Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible.
List Methods for Tuple in python
Below are more python in-built methods for working with Tuple in Python:
|count()||Returns the number of times a specified value occurs in a tuple|
|index()||Searches the tuple for a specified value and returns the position of where it was found|
What is difference between list and tuple in python
The main difference between lists and a tuples is the fact that lists are mutable whereas tuples are immutable. and the other differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.