a=(1,2,3) #tuple
a=[1,2,3] #list
- Lists are mutable objects, tuples are immutable. Length of list can be changed, length of tuple - not. As a consequence, there are more built-in functions for lists in comparison with that for tuples.
a = (1,2,3)
a[0]=10 # -> this line will cause an error
However, you can modify mutable objects inside of immutable ones, like
>>> b=(1,[1,2],3)
>>> b[1].append(42)
(1,[1,2,42],3)
- All empty tuples are the same empty tuple, all empty lists are different lists.
- Having the same information stored in tuple and in list, list will require more memory.
a = [1,2,3,4,5,6,7,8,9,0] #120 bytes
b = (1,2,3,4,5,6,7,8,9,0) #104 bytes
- Operations with tuples are a bit faster than operations with lists.
Comments
Leave a comment