What are the key difference between lists and tuples in python

List and tuples both are used to store multiple items in single variable,  list is created using square brackets but  tuples are create using parentheses.Their are difference and similarity between  lists and tuples in python.Let we check difference between lists and tuples in python.

Difference between lists and tuples

1) List are mutable but tuples are immutable, mutable means that can be modified ,automatically immutable means that can not be modified,So list can be modified, appended , we cant do any of this with tuples. suppose if my_list is already exists ,any of the element from the given list can be reassigned.but we cant reassign tuple elements.

2) Since list is mutable list having additional functionalities like insert , sort and pop are available.But tuples doesn’t support any of this functionalities .

3) Tuples are allocated with large block memory, but list having small memory blocks.so when their are large number of elements tuples is faster than List .

4) Tuples has fixed length while list having variable length.So the length of tuples always initial length that is constant.But List length may differ.

Syntax and example of lists in Python

my_list = [2, 4, 6.8,10]
my_list2 = ['even', 4, 6.8,10]
my_list2 [0] = 'old'; it change list to

my_list2 = ['old', 4, 6.8,10]

Syntax and example of tuples in Python

my_tuple = (2, 4, 6.8,10)
my_tuple2 = ('even', 4, 6.8,10)
my_tuple2 [0] = 'old'; it throws an error

Sno.LISTTUPLE
1Lists are defined using square brackets [ ] Tuples are defined in round brackets ( )
2Lists are mutableTuples are immutable
3Lists are many built-in methods: Since list is mutable list having additional functionalities like insert , sort and pop are available.Tuples are less in-built methods: tuples doesn’t support any of this functionalities
4lists having variable lengthTuples has fixed length
5The Lists are less memory efficient than the tuplesThe tuples are more memory efficient than the list

My Thought

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator