#10- Case Study | Understanding the Differences Between Lists, Tuples, and Sets in Python: A Case Study

NUR ARIF
2 min readJan 8, 2023

Oke here is a case study in the form of a program to illustrate the differences between Lists, Tuples, and Sets in Python:

Case study:

Anna is a software developer who is working on a project that involves storing and manipulating data in Python. She has been using Lists to store her data, but she has recently learned about Tuples and Sets and is wondering how these data structures differ from Lists.

Anna writes the following program to test the differences between Lists, Tuples, and Sets:

# Create a List, Tuple, and Set
my_list = [1, 2, 3, 4]
my_tuple = (1, 2, 3, 4)
my_set = {1, 2, 3, 4}
# Try modifying the List
my_list[0] = 5
print(my_list) # Output: [5, 2, 3, 4]
# Try modifying the Tuple
my_tuple[0] = 5
print(my_tuple) # Output: TypeError: 'tuple' object does not support item assignment
# Try modifying the Set
my_set.add(5)
print(my_set) # Output: {1, 2, 3, 4, 5}
# Try adding a duplicate item to the Set
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4, 5} (no change, because 4 is already in the Set)
# Try accessing an item by index in the List and Tuple
print(my_list[0]) # Output: 5
print(my_tuple[0]) # Output: 1
# Try accessing an item by index in the Set
print(my_set[0]) # Output: TypeError: 'set' object does not support indexing

When Anna runs the program, she observes the following output:

[5, 2, 3, 4]
TypeError: 'tuple' object does not support item assignment
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
5
1
TypeError: 'set' object does not support indexing

From the output, Anna can see that:

Lists are mutable and can be modified, while Tuples are immutable and cannot be modified.

Sets are mutable and can have items added or removed, but do not maintain a specific order and do not allow duplicates.

Lists and Tuples can be accessed using their index, while Sets do not support indexing.

Anna concludes that Lists, Tuples, and Sets are different data structures with their own unique properties and uses. She decides to use Lists for storing and manipulating data that needs to be ordered and modified, Tuples for storing data that needs to be ordered but should not be modified, and Sets for storing unique items that do not need to be ordered.

--

--

NUR ARIF

Backend | Data Scraping | Content Writer | Python Programming | Passionate Cyber Security