List, Tuple, Set, and Dictionary data types and use cases

Mohammad Naimur Rahman
Learn Python Programming
4 min readMar 22, 2021

Python sequence and mapping data types and use cases

List, Tuple, Set, Dictionary are some of the most commonly used data types in python. All those data types have specific advantages depending on the type of operation that needs to be performed. Choosing the right type of data is very important as this can lead to high execution time and inefficient memory usages. This article will cover those sequence and mapping data types and it’s use cases.

One of the most important terms for data types is mutability. The term mutable means the new item(s) can be added or existing item(s) can be updated or deleted from the data structure. Immutable means items or elements are fixed in the data structured insert, update, and delete is not permitted.

List:

A list is a collection of elements. The items in a list are separated by a comma.

  • Items in the list can be accessed by their index e.g. a[0] = 1.
  • The list is mutable. So, items can be added, updated, and deleted from the list.
  • As the list is mutable, hence accessing or iterating items from the list has more time complexity due to its dynamic nature over a tuple.
  • The list can store any type of data element e.g. int, str, etc.
>>> a = [1,2,3,4,5,6]
>>> type(a)
<class 'list'>

The list is useful over tuple when we need to perform add or delete on the data structure. Because of this dynamic nature choosing a list over a tuple will increase the runtime of the program while accessing or iterating items from the list.

Tuple:

A tuple is a collection of data elements like a list. The items in a tuple are separated by a comma. However, the major difference is, a tuple is immutable.

>>> a = (1,2,3,4,5,6)
>>> type(a)
<class 'tuple'>
>>> len(a)
6
  • Items in the tuple can be accessed by its index e.g. a[0] =1
  • Items can’t be added or deleted once the tuple is defined.
  • Also, items can’t be updated in a tuple
>>> a
(1, 2, 3, 4, 5)
>>> a[0]
1
>>> a[0] = 6
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
  • The tuple can store any type of data element e.g. int, str, etc

The tuple is preferred over a list when we need to deal with a fixed data type e.g. [‘MON’, ‘TUE’, ‘WED’, ‘THU’, ‘FRI’, ‘SAT’, ‘SUN’]. Because of the immutable nature, a tuple is efficient in terms of the runtime of the program while accessing or iterating items from it.

Set:

Set is an unordered collection of data elements. Items in a set are separated by a comma.

>>> a = {1,2,3,4,5,6}
>>> type(a)
<class 'set'>
>>> len(a)
6
  • set is an unordered collection of data elements. So, items in the set can’t be accessed by its index.
>>> a = {1,2,3,4,5,6}
>>> a[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable
  • set does not allow duplicates. So adding an existing element to the set will not make any change. However, items in a set can be deleted/removed.
>>> a = {1,2,3,4,5,6}
>>> a.add(1)
>>> a
{1, 2, 3, 4, 5, 6}
>>> a.remove(1)
>>> a
{2, 3, 4, 5, 6}

Set is really useful when a programmer needs to check whether an item exists in the data structure. This leads to an efficient time complexity over tuple or list.

frozenset():

frozenset() is an immutable version of a set. Using frozenset() function, a given iterable can be converted to a frozen set. frozenset does not allow add, update, and delete operation on it.

>>> a =frozenset({1,2,3,4,5,6})
>>> a
frozenset({1, 2, 3, 4, 5, 6})
>>> a.add(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> a.remove(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'remove'

Dictionary (dict):

Dictionary is a data type that stores data values as key: values pair. Dictionary is written in curly brackets with comma-separated key: value pair.

>>> a = {'a':1,'b':2,'c':3}
>>> type(a)
<class 'dict'>
>>> len(a)
3
  • dict is an ordered collection of elements that allow the addition, update, and deletion of items.
  • dict allows retrieving values using keys. keys and values can be accessed by the following. e.g. a.keys() gives all the keys, a.values() return all the values.
>>> a.keys()
dict_keys(['a', 'b', 'c'])
>>> a.values()
dict_values([1, 2, 3])
  • A key is unique to a dictionary meaning key can’t be duplicated in the same dictionary. Also, a value can be retrieved by providing a corresponding key.
>>> a['a']
1
  • A key can be any type of data element e.g. int, str, etc. However, values for a key can be int, str, list(), set(), tuple.
>>> a = {'a':1,2:'b','c':[1,2,3]}
>>> a['a']
1
>>> a['c']
[1, 2, 3]
>>> a[2]
'b'

This article provides a basic idea about list, tuple, set, and dictionary in python.

Thank you for reading this article. Please feel free to provide feedback and post questions in the comments.

--

--