🔹 Built-in Data Structures in Python

Definition:

A Data Structure is a way of organizing and storing data so that it can be accessed and modified efficiently. Different types of data structures are suited to different kinds of applications.

🔹 Built-in Data Structures:

  • List
  • Tuple
  • Dictionaries
  • Set

1. List

A list is a mutable, ordered collection that can hold heterogeneous elements and allows duplicates.

  • Mutable: Can be changed after creation.
  • Ordered: Items have a defined order and can be accessed by index.
  • Heterogeneous:
  • Duplicates Allowed: Same value can appear more than once.
my_list = [1, 2, 3]
my_list.append(4)  # Now [1, 2, 3, 4]
print(my_list[0])  # Output: 1
mixed = [1, "hello", 3.14, True]
nums = [1, 2, 2, 3]
    
Creating a list
fruits = ["apple", "banana", "cherry"]
Accessing Elements:
print(fruits[0])  # Output: "apple"
print(fruits[1:3])  # Output: ["banana", "cherry"]
    
List Methods
fruits.append("orange")
fruits.insert(1, "kiwi")
fruits.remove("banana")
fruits.sort()
    

2. Tuple

A tuple is an ordered, unchangeable (immutable) collection that can store heterogeneous elements and allows duplicates. Created with parentheses ():

  • Ordered: Maintains the order of items.
  • Unchangeable (Immutable): Cannot be modified after creation.
  • Heterogeneous: Can include multiple data types.
  • Duplicates Allowed: Repeating values are fine.
colors = ("red", "green", "blue")
print(colors[1])  # Output: green
    

3. Dictionary

A dictionary is a mutable, key-value paired collection.

  • Unordered / Ordered: In Python 3.7+, insertion order is preserved. Prior to Python version 3.7, it was unordered.
  • Unique Keys: Keys must be unique; values can be duplicated.
  • Mutable: Can add, modify, or delete key-value pairs.
student = {"name": "Alice", "age": 22, "grade": "A"}
student["major"] = "Computer Science"
student.pop("grade")
    

4. Set

A set is an unordered, unique collection of immutable elements.

  • Unordered: No fixed order, so no indexing.
  • Unchangeable (Immutable elements): Elements must be immutable (e.g., no lists inside sets).
  • Unique: Does not allow duplicates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2)  # Union
print(set1 & set2)  # Intersection
print(set1 - set2)  # Difference
    

Practice Task:

1. Create a list of numbers, remove one item, sort the list, and print the result.

numbers = [4, 2, 7, 1, 9]
numbers.remove(7)
numbers.sort()
print("Updated list:", numbers)

2. Create a tuple with 3 favorite colors and print the second color.

colors = ("red", "blue", "green")
print("Second color:", colors[1])

3. Create two sets of numbers and find their intersection and union.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
intersection = set1 & set2
union = set1 | set2
print("Intersection:", intersection)
print("Union:", union)

4. Create a dictionary with information about a book (title, author, year) and print the title.

book = {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925}
print("Title:", book["title"])
    

Comprehensions:

Comprehensions in Python provide a concise way to create lists, dictionaries, and sets. They help make code more readable and efficient by replacing traditional loops with a more compact syntax.

List Comprehensions:

List comprehensions allow you to create a new list by applying an expression to each item in an iterable.

squares = [x**2 for x in range(5)]
print(squares)  # Output: [0, 1, 4, 9, 16]

Dictionary Comprehensions:

Dictionary comprehensions allow the creation of dictionaries using a similar syntax to list comprehensions.

square_dict = {x: x**2 for x in range(5)}
print(square_dict)  # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Set Comprehensions:

Set comprehensions work similarly to list comprehensions but create sets instead of lists.

unique_squares = {x**2 for x in range(5)}
print(unique_squares)  # Output: {0, 1, 4, 9, 16}

Summary

  • Lists: Mutable, ordered, and versatile.
  • Tuples: Immutable and used for fixed collections.
  • Dictionaries: Key-value pairs, unordered, and fast for lookups.
  • Sets: Unordered collections of unique elements.
  • Comprehensions: Provide concise, readable ways to create data structures.