Module 2 of 21 · Advanced Python for AI Development · Intermediate

Data Structures and Algorithms

Duration: 8 min

This module delves into the essential data structures and algorithms that form the backbone of efficient AI development in Python. Understanding these concepts is crucial as they directly impact the performance and scalability of AI applications. Mastery of these topics will enable you to write more efficient code and make informed decisions about which data structures and algorithms to use in various scenarios.

Understanding Lists and Dictionaries

Lists and dictionaries are fundamental data structures in Python. Lists are ordered collections that are mutable, making them suitable for tasks that require maintaining the order of elements. Dictionaries, on the other hand, are unordered collections of key-value pairs, ideal for quick lookups and when the order of elements is not important. Together, they provide the flexibility needed for various AI tasks, such as data preprocessing and feature engineering.

example1.py

numbers = [1, 2, 3, 4, 5]  # A list of integers

# Accessing elements in a list
print(numbers[0])  # Output: 1

# Modifying elements in a list
numbers[0] = 10
print(numbers)  # Output: [10, 2, 3, 4, 5]


student = {'name': 'Alice', 'age': 21,'major': 'Computer Science'}  # A dictionary

# Accessing elements in a dictionary
print(student['name'])  # Output: Alice

# Modifying elements in a dictionary
student['age'] = 22
print(student)  # Output: {'name': 'Alice', 'age': 22,'major': 'Computer Science'}

Try it in Google Colab: Open in Colab

1
[10, 2, 3, 4, 5]
Alice
{'name': 'Alice', 'age': 22,'major': 'Computer Science'}

Exploring Sorting Algorithms

Sorting algorithms are critical for organizing data efficiently, which is a common requirement in AI. Bubble sort and quicksort are two fundamental sorting algorithms. Bubble sort is simple but inefficient for large datasets, while quicksort is more efficient and widely used. Understanding these algorithms helps in choosing the right sorting method based on the specific requirements of the task at hand.

example2.py

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr


def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)


arr = [64, 34, 25, 12, 22, 11, 90]
print(bubble_sort(arr))  # Output: [11, 12, 22, 25, 34, 64, 90]
print(quicksort(arr))  # Output: [11, 12, 22, 25, 34, 64, 90]

💡 Tip: When choosing a sorting algorithm, consider the size and nature of your data. For small datasets, bubble sort might be sufficient, but for larger datasets, quicksort or other efficient algorithms like mergesort or heapsort are recommended.

❓ What is the primary advantage of using a dictionary over a list?

❓ Which sorting algorithm is more efficient for large datasets?

← Previous Continue interactively → Next →

Related Courses