Mastering Python: Unlocking the Power of Lists, Dictionaries, Tuples, and Sets

Python programmers should be familiar with four fundamental data structures: lists, dictionaries, tuples and sets. Each of these structures has unique properties and use cases. In this article, we’ll delve into the details of these data structures, covering their creation, usage, and common operations.

Python Collections

There are four collection data types in the Python programming language:

  • Dictionary is a collection that is ordered** and changeable. No duplicate members.
  • A list is a collection that is ordered and changeable. Allows duplicate members.
  • Tuple is a collection that is ordered and unchangeable. Allows duplicate members.
  • Set is a collection that is unordered, unchangeable*, and unindexed. No duplicate members.
PropertiesDictionaryListSetTuple
DuplicateNoYesNoYes
MutableYesYesYesNo
OrderedYesYesNoYes
IndexedYesYesNoYes
Symbol{}[]{}()
Example{‘key1’:”value”}[1,2,3]{1,2,3}(1,2,3)
DefinitionKey-Value PairsOrdered CollectionUnique CollectionImmutable List

Note:

  • Sets are mutable (can add or remove elements only), but we can’t change the elements of a set
  • Tuples are non-mutable, we cannot change, add or remove items after the tuple has been created.
  • Dictionaries are indexed by keys, while lists and tuples are indexed by a range of numbers
    • Dict[‘name’]
    • mylist[0] or mytuple[2]
  • As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

To Remember

  • A list is like a todo list with a specific order;
  • A tuple is like a list, where one can’t change items in the list
  • Set where duplicates are not allowed
  • A dictionary is simply Key-Value Pairs
Data StructureAccess TimeSearch TimeInsertion TimeDeletion Time
ListO(1)O(n)O(1)(append), O(n) (insert at position)O(n)(remove by value), O(n)(remove by index)
SetO(1)O(1)O(1)O(1)
TupleO(1)O(n)Not applicableNot applicable
DictionaryO(1)O(1)O(1)O(1)

Read about time complexity

Note

  • In sets, search time is O(1) on average because they are implemented using a hash table.

Dictionary

  • Collection of key-value pairs
  • Duplicates are not allowed
  • Ordered
  • Indexed based on keys
  • {}

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

dictionary

Accessing Dictionary Elements

# Example of a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}

# Accessing the value associated with the key 'name'
print(my_dict["name"])  # Output: Alice

Modifying a Dictionary

# Changing the value of the key 'age'
my_dict["age"] = 26
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

Adding and Removing Key-Value Pairs

# Adding a new key-value pair
my_dict["email"] = "alice@example.com"
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York', 'email': 'alice@example.com'}

# Removing a key-value pair
del my_dict["city"]
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'email': 'alice@example.com'}

Dictionary Methods

keys = my_dict.keys()
print(keys)  # Output: dict_keys(['name', 'age', 'email'])

values = my_dict.values()
print(values)  # Output: dict_values(['Alice', 26, 'alice@example.com'])

items = my_dict.items()
print(items)  # Output: dict_items([('name', 'Alice'), ('age', 26), ('email', 'alice@example.com')])

age = my_dict.get("age")
print(age)  # Output: 26

city = my_dict.get("city", "Unknown")
print(city)  # Output: Unknown

email = my_dict.pop("email")
print(email)  # Output: alice@example.com
print(my_dict)  # Output: {'name': 'Alice', 'age': 26}

Lists

  • Ordered collection of items
  • Mutable
  • Allow Duplicates
  • Indexed
  • []
list

Accessing List Elements

# Example of a list
my_list = [1, 2, 3, "Python", 4.5]

# Accessing the first element
print(my_list[0])  # Output: 1

# Accessing the fourth element
print(my_list[3])  # Output: Python

Modifying a List

# Changing the second element
my_list[1] = "Changed"
print(my_list)  # Output: [1, 'Changed', 3, 'Python', 4.5]

List Methods

my_list.append(6)
print(my_list)  # Output: [1, 'Changed', 3, 'Python', 4.5, 6]

my_list.insert(2, "Insert")
print(my_list)  # Output: [1, 'Changed', 'Insert', 3, 'Python', 4.5, 6]

my_list.remove(3)
print(my_list)  # Output: [1, 'Changed', 'Insert', 'Python', 4.5, 6]

element = my_list.pop(1)
print(element)  # Output: Changed
print(my_list)  # Output: [1, 'Insert', 'Python', 4.5, 6]

numbers = [4, 2, 9, 1]
numbers.sort()
print(numbers)  # Output: [1, 2, 4, 9]

numbers.reverse()
print(numbers)  # Output: [9, 4, 2, 1]

Set

  • Unordered
  • Duplicates not allowed
  • Indexing not available
  • Mutable
sets

Creating a Set

# Example of a set
my_set = {1, 2, 3, 4, 5}

# Another way to create a set
another_set = set([1, 2, 3, 4, 5])

Adding and Removing Items

# Adding an item
my_set.add(6)
print(my_set)  # Output: {1, 2, 3, 4, 5, 6}

# Removing an item
my_set.remove(2)
print(my_set)  # Output: {1, 3, 4, 5, 6}

# Removing an item that may or may not

Set Methods

copy_set = my_set.copy()
print(copy_set)  # Output: {1, 3, 4, 5, 6}

my_set.clear()
print(my_set)  # Output: set()

my_set = {1, 2, 3}
element = my_set.pop()
print(element)  # Output: 1 (or 2 or 3, depending on the internal order)
print(my_set)   # Output: {2, 3} (or {1, 3} or {1, 2})

Tuples

  • Ordered
  • Immutable
  • Duplicates allowed
  • Indexed by number
tuples

Accessing Tuple Elements

# Example of a tuple
my_tuple = (1, 2, 3, "Python", 4.5)

# Creating a tuple with a single element
single_element_tuple = (5,)

# Accessing the first element
print(my_tuple[0])  # Output: 1

# Accessing the last element
print(my_tuple[-1])  # Output: 4.5

Tuple Unpacking

# Unpacking a tuple
a, b, c, d, e = my_tuple
print(a)  # Output: 1
print(d)  # Output: Python

Immutability of Tuples

# Trying to change an element will result in an error
# my_tuple[1] = "Changed"  # This will raise a TypeError

# However, you can concatenate tuples to create a new one
new_tuple = my_tuple + ("New Element",)
print(new_tuple)  # Output: (1, 2, 3, 'Python', 4.5, 'New Element')

Tuple Methods

count = my_tuple.count(2)
print(count)  # Output: 1

index = my_tuple.index("Python")
print(index)  # Output: 3

Tuple Packing and Unpacking

Tuple packing refers to the process of assigning multiple values to a single tuple, while tuple unpacking is the reverse operation.

# Packing a tuple
packed_tuple = 1, 2, "Python"

# Unpacking a tuple
x, y, z = packed_tuple
print(x)  # Output: 1
print(z)  # Output: Python

Tuples vs. Lists

While both tuples and lists are used to store collections of items, the choice between them often depends on whether you need the collection to be mutable or immutable.

  • Mutability: Lists are mutable (modifiable), while tuples are immutable.
  • Performance: Tuples can be more memory-efficient and slightly faster than lists, especially when dealing with large collections of data that do not need to be modified.
  • Usage: Use tuples when you want to ensure that the data remains constant, such as in cases where the structure of data is integral to the program (e.g., coordinates, RGB values).

Tuples as keys in dictionaries (since they are hashable).

Tuples are hashable in Python, which makes them valid keys for dictionaries. This is because tuples are immutable, meaning their contents cannot be changed after they are created. Hashable objects, like tuples, have a __hash__() method that allows Python to generate a unique hash value, making them suitable for use as dictionary keys.
# Using a tuple as a key in a dictionary
coordinates = {}

# Adding a tuple key
coordinates[(10, 20)] = "Point A"
coordinates[(30, 40)] = "Point B"

# Accessing values using tuple keys
print(coordinates[(10, 20)])  # Output: Point A
print(coordinates[(30, 40)])  # Output: Point B

In this example, (10, 20) and (30, 40) are tuples used as keys in the coordinates dictionary. You can use these keys to retrieve the associated values.

However, it’s important to remember that only immutable tuples (i.e., those containing immutable elements like strings, numbers, or other tuples) can be used as dictionary keys. If a tuple contains mutable elements like lists, it won’t be hashable and cannot be used as a key.

Functions that return multiple values can use tuples

In Python, a function can return multiple values using tuples. When you return multiple values separated by commas, Python automatically packs them into a tuple. The caller can then unpack these values into separate variables.

def get_coordinates():
    x = 10
    y = 20
    return x, y

coordinates = get_coordinates()
print(coordinates)  # Output: (10, 20)

# Unpacking the tuple into separate variables
x_coord, y_coord = get_coordinates()
print(x_coord)  # Output: 10
print(y_coord)  # Output: 20

Underlying data structures

e’s a comparison of the data structures in Python in a table format:

Data StructureUnderlying Structure
ListDynamic Array
TupleStatic Array
SetHash Table
DictionaryHash Table

Use case

Sets

  • Removing duplicates from a list.
  • Checking membership and performing set operations like union, intersection, and difference.

Dictionaries

  • Storing and retrieving data by keys.
  • Implementing lookups, caching, and associative arrays.
  • Representing JSON-like data structures.

Tuples

  • Storing fixed collections of items where immutability is required.
  • Using tuples as keys in dictionaries (since they are washable).
  • Returning multiple values from a function.

List

  • Storing and manipulating collections of items.
  • Iterating over elements to perform operations.
  • Implementing stacks and queues.

Example

Healthcare System

List:

  • Patient Appointments: A list to manage daily appointments at a clinic. Each patient’s appointment is an item on the list.
appointments = ['John Doe - 10:00 AM', 'Jane Smith - 10:30 AM', 'Alice Johnson - 11:00 AM']

Dictionaries:

  • Patient Records: A dictionary where each patient’s ID is the key, and their medical history, contact details, and prescriptions are the values.
patient_records = {
    'P001': {'name': 'John Doe', 'age': 45, 'conditions': ['Diabetes'], 'medications': ['Metformin']},
    'P002': {'name': 'Jane Smith', 'age': 30, 'conditions': ['Hypertension'], 'medications': ['Lisinopril']}
}

Tuples

  • Lab Test Results: A tuple to store immutable data such as the results of a specific lab test (e.g., cholesterol levels).
cholesterol_test = ('Cholesterol', 180, 'mg/dL')

Set

  • Unique Medications: A set to manage a collection of unique medications prescribed at the clinic to avoid duplicates.
prescribed_medications = {'Metformin', 'Lisinopril', 'Atorvastatin'}

E-commerce Platform

Lists:

  • Shopping Cart: A list to store items a user intends to purchase. Each item is an entry in the list.
shopping_cart = ['Laptop', 'Wireless Mouse', 'Keyboard']

Dictionaries:

  • Product Catalog: A dictionary where product IDs are keys and the corresponding product details (name, price, stock) are values.
product_catalog = {
    'P001': {'name': 'Laptop', 'price': 999.99, 'stock': 20},
    'P002': {'name': 'Wireless Mouse', 'price': 19.99, 'stock': 150}
}

Tuples:

  • Customer Order: A tuple to store order details that should not change, like the order number and date.
order_details = ('Order123', '2024-08-29')

Sets:

  • Unique Customers: A set to track unique customers who have purchased, ensuring no duplicate entries.
unique_customers = {'Customer001', 'Customer002', 'Customer003'}

Educational System

Lists

  • Classroom Attendance: A list to keep track of students present in a classroom.
attendance_list = ['Alice', 'Bob', 'Charlie']

Dictionaries

  • Grades Book: A dictionary where each student’s name is the key and their grades are the values.
grades = {
    'Alice': {'Math': 95, 'Science': 88},
    'Bob': {'Math': 78, 'Science': 82},
    'Charlie': {'Math': 85, 'Science': 91}
}

Tuples:

  • Course Details: A tuple to store unchangeable course information like course ID and title.
course_details = ('CS101', 'Introduction to Computer Science')

Sets:

  • Extracurricular Participants: A set to manage students participating in extracurricular activities, ensuring each student is listed only once.
extracurricular_participants = {'Alice', 'David', 'Eva'}

Social Media Platform

Lists:

  • User’s Friends: A list to maintain the order of friends in a user’s friend list.
friends_list = ['Alice', 'Bob', 'Charlie']

Dictionaries:

  • User Profiles: A dictionary where each user’s username is the key and their profile information (name, age, bio) is the value.
user_profiles = {
    'user1': {'name': 'Alice', 'age': 28, 'bio': 'Love traveling and photography'},
    'user2': {'name': 'Bob', 'age': 34, 'bio': 'Avid reader and gamer'}
}

Tuples:

  • Post Metadata: A tuple to store immutable metadata about a post, such as post ID and timestamp.
post_metadata = ('Post123', '2024-08-29 10:15:00')

Sets:

  • Liked Posts: A set to manage posts that a user has liked, ensuring no duplicate likes.
liked_posts = {'Post123', 'Post456', 'Post789'}

Conclusion

Python’s data structures—lists, dictionaries, sets, and tuples—each have unique characteristics and use cases. Lists and dictionaries are mutable, making them ideal for dynamic data. Sets provide unique, unordered collections optimized for mathematical operations. Tuples, with their immutability, are perfect for storing related data that should not change. Understanding these data structures will greatly enhance your ability to write efficient and effective Python code.

Resources

1 thought on “Mastering Python: Unlocking the Power of Lists, Dictionaries, Tuples, and Sets”

Leave a Comment