DEV Community

ROHITH
ROHITH

Posted on

Mastering Advanced Data Types in Python for AI/ML

Welcome back, everyone! In our last session, we explored foundational data types essential for AI/ML engineering, data analytics, and data science. Today, we're advancing our knowledge by diving into two more powerful and widely applicable data structures: Sets and Dictionaries.

Python Sets: Collections of Unique & Unordered Elements

A Python Set is conceptually similar to a mathematical set, characterized by two primary features:

  • Uniqueness: Sets inherently store only unique elements. Attempting to add a duplicate element will simply be ignored, and the set will remain unchanged.
  • Unordered: Unlike lists or tuples, elements within a set do not maintain any specific order. This implies that elements cannot be accessed using an index.

Set Creation and Basic Operations

Sets are typically created using curly braces {}. To create an empty set, you must use the set() constructor, as {} alone creates an empty dictionary.

# Creating a set with diverse elements
my_set = {2, 'a', 3.14}
print(my_set) # Output order may vary, e.g., {'a', 2, 3.14}

# Creating an empty set
empty_set = set()
Enter fullscreen mode Exit fullscreen mode

Modifying sets involves adding or removing elements:

  • Adding Elements (.add()): Elements are added quickly due to internal hashing. Duplicates are ignored.

    my_set.add(3)
    my_set.add(2) # '2' already exists, no change
    print(my_set)
    
  • Removing Elements (.remove()): Elements are removed by their value. If the element doesn't exist, a KeyError is raised.

    my_set.remove(3.14)
    # my_set.remove(5) # This would cause a KeyError
    
  • Important Constraint: Sets can only contain immutable objects (e.g., numbers, strings, tuples). Mutable objects like lists are not allowed and will raise a TypeError.

Advanced Set Operations

Sets offer powerful operations for comparing and combining collections:

  • Intersection (.intersection() or &): Finds common elements between sets.

    set_A = {1, 2, 3, 'a'}
    set_B = {3, 'a', 'b', 4}
    print(set_A.intersection(set_B)) # Output: {'a', 3}
    
  • Union (.union() or |): Combines all unique elements from both sets.

    print(set_A.union(set_B)) # Output: {1, 2, 3, 4, 'a', 'b'}
    
  • Symmetric Difference (.symmetric_difference() or ^): Returns elements unique to either set (not in both).

    print(set_A.symmetric_difference(set_B)) # Output: {1, 2, 'b', 4}
    
  • Difference (.difference() or -): Returns elements present in the first set but not in the second.

    print(set_A.difference(set_B)) # Output: {1, 2}
    

When to Use Sets: Ideal for managing unique elements, performing fast membership checks, and efficient set-theoretic operations where element order is irrelevant.

Python Dictionaries: Versatile Key-Value Storage

Dictionaries are indispensable data structures, particularly prevalent in professional applications due to their efficient key-value storage mechanism.

Structure and Access

A dictionary stores data as key: value pairs within curly braces {}.

  • Keys: Must be unique and immutable (similar to set elements).
  • Values: Can be any Python data type, including mutable ones like lists or other dictionaries.
# Storing customer phone numbers
name_to_phone = {
    "Alex": "123-456-7890",
    "Nick": "987-654-3210"
}
print(name_to_phone)
Enter fullscreen mode Exit fullscreen mode

Values are accessed directly using their corresponding key:

alex_phone = name_to_phone["Alex"]
print(alex_phone) # Output: 123-456-7890
Enter fullscreen mode Exit fullscreen mode

Safe Access and Modification

Directly accessing a key that doesn't exist will cause a KeyError. To avoid this, use the .get() method, which allows you to specify a default return value.

# Accessing a potentially missing key safely
maria_info = name_to_phone.get("Maria", "No Info Available")
print(maria_info) # Output: No Info Available
Enter fullscreen mode Exit fullscreen mode
  • Adding/Updating Elements: New key-value pairs are added by assignment. If the key already exists, its value is updated.

    name_to_phone["Sara"] = "555-123-4567" # Add Sara
    name_to_phone["Sara"] = "Hidden"      # Update Sara's number
    print(name_to_phone)
    
  • Checking Key Existence: Use the in operator to check if a key exists, returning True or False.

    print("Sara" in name_to_phone) # Output: True
    

When to Use Dictionaries: Perfect for mapping unique keys to values, rapid data retrieval, and representing structured records.

Solidify Your Skills: Python Homework & Resources

To deepen your understanding and apply what you've learned, we've prepared a comprehensive Python homework set!

  • Access the Homework: Find the link to the GitHub repository in the video description.
  • What You'll Practice: The tasks cover fundamental operations, various variable types (strings, booleans), data structures like tuples, sets, and dictionaries, and control flow (loops, conditionals).
  • Daily Uploads: New lessons and tasks will be uploaded daily at 7:00 PM KAN time (GMT+5).

Keep Learning and Building!

We're dedicated to empowering you with the AI/ML, data science, and data analytics skills vital for your career. Whether you aspire to create your own startup or join a leading tech company, these foundational skills are your stepping stone.

If this session helped clarify these powerful data types, please show your support by starring our GitHub repository and liking this video on YouTube. Your engagement fuels our mission to provide free educational content.

See you in the next class! Cheers!

Top comments (0)