List, Set and Tuple are three of the core data types in Python.  All the three types are used to store collections of data. They are similar in that they can store multiple elements, but they differ in their properties and uses. In this article we will compare the three types on various basis.

Definitions

A list is a mutable collection of ordered elements. They are normally used for storing dynamic information that can change in size, as elements can be added, removed, or modified. Lists can contain any type of data, such as integers, strings, sets, or even other lists.   

A set is a mutable collection of unordered, unique elements. A set cannot contain duplicate elements and the elements in a set do not have a specific order. For example, a set {1, 2, 3}  can also be written as {3, 1, 2} and it will still be considered the same set.   

A tuple is an immutable collection of ordered elements.  Tuples can contain any type of data, including strings, numbers, lists, and even other tuples. Unlike lists and sets, tuples have a fixed size, meaning that elements cannot be added or removed after the tuple is created.

Syntax

A list is instantiated by enclosing elements within square brackets ([]). Elements in a list can be of any data type.

ExampleEdit & Run
my_list = [1, 2, 'hello', True] 

print(my_list)
Output:
[1, 2, 'hello', True][Finished in 0.010481001110747457s]

A set is defined by enclosing elements within curly braces ({}). Elements in a set must be hashable, this means  that we can't  include mutable objects like lists, dictionaries, or sets themselves as elements. Sets also do not support duplicate elements, meaning that if an element is already present in the set, it will not be added again.

ExampleEdit & Run
my_set = {1, 2, 'Hello', True, (1, 2, 3)}

print(my_set)
Output:
{1, 2, (1, 2, 3), 'Hello'}[Finished in 0.010014903033152223s]
ExampleEdit & Run

A set raises an error if a mutable object is included

#try putting a list an a set

my_set = {[1, 2]}
Output:
Traceback (most recent call last):  File "<string>", line 3, in <module>TypeError: unhashable type: 'list'[Finished in 0.010090741096064448s]

A tuple is defined by enclosing elements within parentheses (()). Like in lists, elements can be of any type.

ExampleEdit & Run
my_tuple = ('Python', 'Java', 'C++', 'Kotlin')
print(my_tuple)
Output:
('Python', 'Java', 'C++', 'Kotlin')[Finished in 0.009758697124198079s]

Mutability

Mutability refers to the ability  of an object or data type to be changed or modified. A mutable object allows its values to be modified after it has been created, while an immutable object does not allow any changes to its values after creation.

Lists are mutable, meaning they can be changed after they are created. Elements can be added, removed, or modified. Example:

ExampleEdit & Run
my_list = [1, 3, 5, 7, 6, 11]

my_list[0] = 2 #update the first element
my_list.append(13) #add an element

#remove an element
my_list.remove(6)

#print the list
print(my_list)
Output:
[2, 3, 5, 7, 11, 13][Finished in 0.009825736051425338s]

Similarly, sets are mutable, elements can be added or removed in-place.

ExampleEdit & Run
my_set = {1, 2, 3, 5, 7}

#add element to the set
my_set.add(11)
my_set.add(13)

#remove an element
my_set.remove(1)

#print the set
print(my_set)
Output:
{2, 3, 5, 7, 11, 13}[Finished in 0.009561094921082258s]

Tuples are immutable, they cannot be changed after they are created. Elements cannot be updated, added or removed.  

ExampleEdit & Run
my_tuple = (1, 2, 3, 4)

#raises an error
my_tuple[0] = 10
Output:
Traceback (most recent call last):  File "<string>", line 4, in <module>TypeError: 'tuple' object does not support item assignment[Finished in 0.009685598080977798s]

Accessing elements

Lists and tuples supports accessing elements using their indices. This way  we can access an element at an arbitrary position.

Both positive indexing and negative indexing is supported in both lists and tuples.

ExampleEdit & Run

accessing elements in a list

my_list = [2, 3, 5, 7, 11, 13, 17]

#access elements
print(my_list[0]) # the first element
print(my_list[3]) #The fourth element
print(my_list[-1]) #The last element
Output:
2717[Finished in 0.009538393002003431s]
ExampleEdit & Run

access elements in a tuple

my_tuple = ('java', 'C++', 'C#', 'Python', 'Kotlin', 'C', 'Ruby')

#access elements
print(my_tuple[0]) # the first element
print(my_tuple[4]) #The fifth element
print(my_tuple[-1]) #The last element
Output:
javaKotlinRuby[Finished in 0.009385110810399055s]

Sets, on the other hand, do not support indexing. Indexing assumes an order in the elements, which sets do not have.  Attempting to access an element in a set using an index will raise an error.

ExampleEdit & Run
my_set = {2, 3, 4}

#raises an error
my_set[0]
Output:
Traceback (most recent call last):  File "<string>", line 4, in <module>TypeError: 'set' object is not subscriptable[Finished in 0.009904888924211264s]

Sets are primarily used for membership testing and storing unique elements, rather than accessing specific elements at a particular index. If you need to access a specific element in a set, you can convert it to a list or tuple first.