Python Programming Training Certification
Flexible Hours
100 Assignments
Instructor Led online Training
50 LMS Access
24X7 Support
100% Skill Level
Enquire Now
Course Overview
Python is one of the world’s top programming languages used today and Python training has become the most popular training across individuals. Training Basket’s Python Training & Certification course covers basic and advanced Python concepts and how to apply them in real-world applications.Python is a flexible and powerful open-source language that is easy to learn and consists of powerful libraries for data analysis and manipulation. Our Python training course content is curated by experts as per the standard Industry curriculum. The curriculum, coding challenges and real-life problems cover data operations in Python, strings, conditional statements, error handling, shell scripting, web scraping and the commonly used Python web framework Django. Take this Python training and certification course and become job-ready now.
Python List Vs Tuple
In this tutorial, we will learn the important difference between the list and tuples and how both are playing a significant role in Python.
Lists and Tuples are used to store one or more Python objects or data-types sequentially. Both can store any data such as integer, float, string, and dictionary. Lists and Tuples are similar in most factors but here we will describe the main difference between them.
Let’s discuss the main differences in the following points.
Representation Differences
The representation of the Lists and tuple is marginally different. List are commonly enclosed with the square bracket [], and elements are comma-separated element. Tuples are enclosed with parenthesis (), and elements are separated by the comma. The parenthesis is optional to use, and these types of tuples are called tuple packing.
Consider the following example.
- list1 = [‘JavaTpoint’, 1, 2, 54.30, {‘Name: ”Peter’}]
print(type(list))
tuple1 = (‘JavaTpoint’,5,8,31.9,[1,2,3])
print(type(tuple1))
Output:
- class ‘list’
class ‘tuple’
In the above program, we defined a list1 variable which holds a list of different data type from index 0 to 4. We defined another variable tuple1, which holds a tuple of different data types. It is enclosed by the ().
Mutable Lists and Immutable Tuples
It is the most important difference between list and tuple whereas lists are mutable, and tuples are immutable. The lists are mutable which means the Python object can be modified after creation, whereas tuples can’t be modified after creation. Consider the given an example.
- a = [“Peter”,”Joseph”,”Mathew”,”Ricky”]
print(a)
Output:
- [‘Peter’, ‘Joseph’, ‘Mathew’, ‘Ricky’]
Now we are changing 0th index element “Peter” to “Samson”.
- a[0] = “Samson”
print(a)
Output:
- [‘Samson’, ‘Joseph’, ‘Mathew’, ‘Ricky’]
Now we create a tuple and do the same thing.
- a = (10,20,”JavaTpoint”,30,40)
print(a)
Output:
- (10, 20, ‘JavaTpoint’, 30, 40)
- a[0] = 50
Output:
- TypeError Traceback (most recent call last)
ipython-input-5-52b2981fae12 in module
—-> 1 a[0] = 50TypeError: ‘tuple’ object does not support item assignment
We get an error while changing the 1st element of the tuple because of immutability. It does not support item assignment.
Debugging
The tuples are easy to debug in a big project because of its immutability. If we have a small project or less number of data, then lists play an effective role. Let’s consider the following example:
- a = [6,9,4,3,7,0,1]
# Copying address of a in b
b = a
a[3] = “JavaToint”
print(a)
Output:
- [6, 9, 4, ‘JavaToint’, 7, 0, 1]
In the above code, we did b = a; here we are not copying the list object from b to a. The b referred to the address of the list a. It means if we make the change in the b then that will reflect the same as in list a, and it makes debugging easy. But it is hard for the significant project where Python objects may have multiple references.
It will be very complicated to track those changes in lists but immutable object tuple can’t change after created.
So tuples are easy to debug.
Functions Support
The tuples support less operation than the list. The inbuilt dir(object) is used to get all the supported functions for the list and tuple.
- List Functions
- dir(list)
Output:
- [‘__add__’,’__class__’,’__contains__’,’__delattr__’,’__delitem__’,’__dir_,
‘__doc__’,’__eq__’,’__format__’, ‘__get__’,’__getattribute__’,’__getitem_’ ‘__gt__’,
‘__hash__’,’__iadd__’,’__imul__’,’__init__’,’__init_subclass__”__iter__’,
‘__le__’,’__len__’,’__lt__’,’__mul__’, ‘__ne__’,’__new__’,
‘__reduce__’, ‘__reduce_ex__’,’__repr__’,’__reversed__’,’__rmul__’,’__setattr__’,
‘__setitem__’,’__sizeof__’,’__str__’,’__subclasshook__’,
‘append’,
‘clear’,
‘copy’,
‘count’,
‘extend’,
‘index’,
‘insert’,
‘pop’,
‘remove’,
‘reverse’,
‘sort’]
- Tuple Functions
- dir(tuple)
Output:
- [‘__add__’,
‘__class__’,
‘__contains__’,
‘__delattr__’,
‘__dir__’,
‘__doc__’,
‘__eq__’,
‘__format__’,
‘__ge__’,
‘__getattribute__’,
‘__getitem__’,
‘__getnewargs__’,
‘__gt__’,
‘__hash__’,
‘__init__’,
‘__init_subclass__’,
‘__iter__’,
‘__le__’,
‘__len__’,
‘__lt__’,
‘__mul__’,
‘__ne__’,
‘__new__’,
‘__reduce__’,
‘__reduce_ex__’,
‘__repr__’,
‘__rmul__’,
‘__setattr__’,
‘__sizeof__’,
‘__str__’,
‘__subclasshook__’,
‘count’,
‘index’]
Memory Efficient
The tuples are more memory efficient than the list because tuple has less built-in operations. Lists are suitable for the fewer elements whereas tuples are a bit faster than the list for the huge amount of data.
- Tuple = (1,2,3,4,5,6,7,8,9,0,5485,87525,955,3343,53234,6423,623456,234535)
List = [1,2,3,4,5,6,7,8,9,0,78,34,43,32,43,55,54,212,642,533,43434,54532 ]
print(‘Tuple size =’, Tuple.__sizeof__()) # Tuple size = 52
print(‘List size =’, List.__sizeof__())
Output:
- Tuple size = 168
List size = 216
Conclusion
- In Some cases, lists might seem more useful than tuples. But tuples are important data structures of the Python. Tuples are commonly used for unchangeable data or we can say that the data will be “write- protected” in the tuples. Tuples sends the indication to the Python interpreter the data should not change in the future.
- We can use tuple the same as a dictionary without using keys to store the data. For example-
- list1 = [(101, “Mike”, 24),(102, ‘Hussey’, 26),(103, ‘David’, 27),(104, ‘Warner’, 29)]
- Tuples can use for the dictionary keys because these are hashable and immutable whereas lists can’t use a keys in dictionary.
- dict = {(“Mike”,22):24000} #valid dictionary
dict = {[“Peter”,26]:25000} #Invalid dictionary
Candidate’s Journey During Our Training Program
Expert’s Advice & Selection of Module
Choosing the right type of module for the training is half the battle & Our Team of experts will help & guide you.
Get Trained
Get Trained & Learn End to End Implementation from our Expert Trainer who are working on the same domain.
Work on Projects
We Do make our student’s work on multiple case studies , scenario based tasks & projects in order to provide real-time exposure to them.
Placements
We have a dedicated placement cell in order to provide placement assistance & relevant interviews to our candididates till selection
Placement Partner