W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

Python List vs Tuple

In Python, both lists and tuples are sequence data types that can store a collection of items. Both can store items of heterogeneous types i.e. each item stored in a list or a tuple can be of any data type.

We can access items in lists and tuples by their indices.

Still, the lists and tuples are very much different. Let’s list down the differences between lists and tuples.

1. Syntax difference

Syntax of list and tuple is slightly different. Lists are surrounded by square brackets [ ] and Tuples are surrounded by parenthesis ( ).

List = [1, 2, 3]
 
Tuple = (1, 2, 3)

2. Mutable lists vs immutable tuples

The main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable. 

It means that we can modify a list after it has been initialized i.e. we can add, update or even delete items in a list. But, we cannot change the items in a tuple once it has been created.

# list
 
List = [1, 2, 3]
 
List.append(4)
 
print(List)     # [1, 2, 3, 4]
 
# tuple
 
Tuple = (1, 2, 3)
 
Tuple.append(4) # AttributeError: 'tuple' object has no attribute 'append'

3. Performance gains

Tuples are immutable so they have lesser built-in operations. This saves us some memory.

Due to lesser operations, tuples are a bit faster but not that much to mention about until we have a huge number of elements.

Tuple = (1,2,3,4,5,6,7,8,9,0)
List = [1,2,3,4,5,6,7,8,9,0]
 
print('Tuple size =', Tuple.__sizeof__())       # Tuple size = 52
print('List size =', List.__sizeof__())         # List size = 60

4. Using as key in dictionary

Tuples can be used as the key in a dictionary due to their hashable and immutable nature. Remember that only immutable values can be hashed.

Lists are not used because list can’t handle __hash__() operation and have mutable nature.

dict1= {('a','b'):123#Valid
 
dict2 = {['a','b']:123}     #Invalid

5. Usecases

Generally, Tuples are heterogeneous data structures so they can be used to create records (value objects) containing different type of associated information. e.g. employee records.

Lists, generally, are homogeneous sequences. In the above case, we can use a list to store records of multiple employees where each employee record is a tuple.

Thus, we can add and remove employees from the list as we proceed further into the program. But we should not be able to edit an employee record, rather we shall create another employee record (new tuple) with existing employee information and then store in the list again.

Having immutable value objects is always recommended in other programming languages as well.

6. Conclusion

Finally, let us list down all the differences between lists and tuples in Python, discussed above.

  1. List are created with square brackets and tuples are created with round bracket.
  2. Lists are mutable whereas Tuples are immutable.
  3. Tuples offer a little more memory efficient solution.
  4. Tuples can be used as keys in dictionary, while Lists cannot be.
  5. Tuples are great for defining and creating Value Objects. Lists are great for storing a collection of value objects.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.