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. | LIST | TUPLE |
1 | Lists are defined using square brackets [ ] | Tuples are defined in round brackets ( ) |
2 | Lists are mutable | Tuples are immutable |
3 | Lists 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 |
4 | lists having variable length | Tuples has fixed length |
5 | The Lists are less memory efficient than the tuples | The tuples are more memory efficient than the list |