Comparing List, Tuple, and Set: Key Differences

Introduction

In this article, we'll explore the differences between lists, tuples, and sets in Python. A basic understanding of Python syntax is assumed. Let's dive in ๐Ÿš€.

Lists

A list is a Python object that stores multiple items of potentially different datatypes. It is created using square brackets or the list constructor, like list(). Here's an example of how to create one:

# Creating a list using the list constructor
car_brand_contruct = list(['Toyota', 'Ford', 'BMW'])
print(car_brand_contruct)

# # Creating a list by assigning empty brackets
car_brand_bracket = ['Toyota', 'Ford', 'BMW']
print(car_brand_bracket)
print(type(car_brand_bracket))

Key Characteristics

  • Ordered: Maintains the order of insertion, facilitating easy access by position.

  • Mutable: Allows addition, removal, and modification of items after creation.

  • Allows Duplicates: Can contain items with the same value and type.

Use Case

Looking at real life problems where sequential order is an importance, removing and adding items is allowable and duplicates are not a problem in its solution. They include Task Management, Recipe steps, Shopping cart, Record Keeping etc

# Creating a Task List
steps = ["Make some pancakes", "Wash up", "Go to the gymn", "Wash up", "Draft Letter to grandma"]

# If you think about it, this would make a good use case of lists
# Order, Mutability and Duplicate items are features of task lists

Tuple

A tuple is a Python object that, like a list, can store multiple items. However, all items in a tuple must be of the same type. Once created, a tuple is immutable and cannot be altered. Here's how to create one:

# Creating a tuple using its constructor
car_brand_contruct = tuple(['Toyota', 'Ford', 'BMW'])
print(car_brand_contruct)

# Creating a tuple by assigning empty brackets
car_brand_bracket = ('Toyota', 'Ford', 'BMW')
print(car_brand_bracket)
print(type(car_brand_bracket))

Key Characteristics

  • Ordered: Preserves the order of creation, enabling access by position.

  • Immutable: Items cannot be added, removed, or changed after creation.

  • Allows Duplicates: Similar items are permitted.

Use Cases:

Tuples are used in scenarios where the value of the items is constant, the items are accessible, and duplicates are allowed. These include Coordinates, Database Records, Fixed Collections, storing constants, and shape parameters.

# Coordinates 
latitude, longitude = (27.986065, 86.922623) # Guess where this leads to ๐Ÿ‘€

# Fixed Collections 
months_of_the_year = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

# Shape Parameters
length, breadth, height = (130, 40, 80)

Set

A set is a Python object capable of holding multiple items, which can be of different types. Unlike lists and tuples, sets are unordered. Here's how to create one:

# Creating a tuple using its constructor
car_brand_contruct = set(['Toyota', 'Ford', 'BMW'])
print(car_brand_contruct)

# Creating a tuple by assigning empty brackets
car_brand_bracket = {'Toyota', 'Ford', 'BMW'}
print(car_brand_bracket)
print(type(car_brand_bracket))

Key Characteristics

  • Unordered: Items do not maintain a specific order; their arrangement can change.

  • Mutable: Supports addition and removal of items but not modification.

  • No Duplicates: Only unique items are allowed.

Use Case

Below are some use cases:

# Removing duplicates some an array list
objects = set(["scissors", "comb", "joystick", "comb", "bag"])

Summary

Here's a quick table summarizing the differences:

ListTupleSet
OrderedOrderedUnordered
MutableImmutableMutable
DuplicatesDuplicatesNo Duplicates
list()tuple()set()
<variable name> = []<variable name> = ()<variable name> = {}

This overview should help you choose the right data structure based on your specific task requirements in Python. If you have any comment on how youโ€™d use any of these, leave a comment ๐Ÿ‘๐Ÿพ. Later๐Ÿ‘‹๐Ÿพ

0
Subscribe to my newsletter

Read articles from Tochukwu Chidi Daniel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tochukwu Chidi Daniel
Tochukwu Chidi Daniel

I am currently undergoing a Computer Science Masters degree program at Northumbria University.