Lists (Already Covered)

Python: python fruits = ["apple", "banana", "cherry"] fruits.append("date") print(fruits) ['apple', 'banana', 'cherry', 'date']

TrackJava to Python Journey
Current SectionPython Basics
Progress8 of 19

1. Lists (Already Covered)

Python:

fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits)  # ['apple', 'banana', 'cherry', 'date']

Characteristics:

  • Ordered, mutable (changeable), allows duplicates, indexed access.
  • Equivalent to Java's ArrayList<T>.

2. Tuples

Python:

coordinates = (10, 20)
rgb = (255, 128, 64)
single = (42,)  # Note the comma for single-element tuple

print(coordinates[0])  # 10
print(len(rgb))        # 3

Characteristics:

  • Ordered, immutable (cannot be changed after creation).
  • Allows duplicates and indexed access.
  • Cannot add, remove, or modify elements.
  • Lighter than lists; use when data shouldn't change.
  • Often used for returning multiple values from functions.

Java Equivalent:

// No direct equivalent; closest is using immutable collections
final int[] coordinates = {10, 20};  // But still mutable

Why Use Tuples:

def get_user_info():
    return ("Alice", 25, "alice@example.com")  # Return multiple values

name, age, email = get_user_info()
print(name)  # Alice

Iteration:

for x in (1, 2, 3):
    print(x)  # 1, 2, 3

3. Sets

Python:

unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers)  # {1, 2, 3, 4, 5}

# Adding elements
unique_numbers.add(6)

# Removing elements
unique_numbers.remove(3)

# Duplicates are automatically ignored
tags = {"python", "java", "python", "rust"}
print(tags)  # {'python', 'java', 'rust'} (only 3 unique elements)

Characteristics:

  • Unordered, mutable, no duplicates.
  • No indexed access (cannot do set[0]).
  • Extremely fast membership testing (in operator).
  • Great for removing duplicates or checking membership.

Java Equivalent:

Set<Integer> uniqueNumbers = new HashSet<>();
uniqueNumbers.add(1);
uniqueNumbers.add(2);
uniqueNumbers.add(3);

Set Operations:

set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

union = set_a | set_b        # {1, 2, 3, 4, 5, 6}
intersection = set_a & set_b  # {3, 4}
difference = set_a - set_b    # {1, 2}
symmetric_diff = set_a ^ set_b  # {1, 2, 5, 6}

print(set_a.union(set_b))          # Same as |
print(set_a.intersection(set_b))   # Same as &

Removing Duplicates:

numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5]
unique = list(set(numbers))
print(unique)  # [1, 2, 3, 4, 5] (order may vary)

4. Dictionaries

Python:

student = {
    "name": "Alice",
    "age": 25,
    "grade": "A"
}

print(student["name"])  # Alice
student["age"] = 26     # Modify
student["email"] = "alice@example.com"  # Add new key-value pair

Characteristics:

  • Unordered (Python 3.7+ maintains insertion order), mutable.
  • Key-value pairs, keys are unique and immutable.
  • Fast lookup by key.
  • Equivalent to Java's HashMap<K, V>.

Java Equivalent:

Map<String, Object> student = new HashMap<>();
student.put("name", "Alice");
student.put("age", 25);
student.put("grade", "A");

System.out.println(student.get("name"));  // Alice

Common Dictionary Operations:

# Check if key exists
if "name" in student:
    print(student["name"])

# Get value with default
email = student.get("email", "not provided")  # Returns "not provided" if key doesn't exist

# Get all keys
keys = student.keys()  # dict_keys(['name', 'age', 'grade', 'email'])

# Get all values
values = student.values()  # dict_values(['Alice', 26, 'A', 'alice@example.com'])

# Get all key-value pairs
items = student.items()  # dict_items([('name', 'Alice'), ...])

# Remove a key-value pair
del student["email"]
# or: student.pop("email")

# Clear all entries
# student.clear()

# Iterate over dictionary
for key, value in student.items():
    print(f"{key}: {value}")

Nested Dictionaries:

company = {
    "name": "TechCorp",
    "employees": [
        {"id": 1, "name": "Alice"},
        {"id": 2, "name": "Bob"}
    ],
    "address": {
        "city": "San Francisco",
        "zip": "94105"
    }
}

print(company["address"]["city"])  # San Francisco
print(company["employees"][0]["name"])  # Alice

5. Frozen Sets

Python:

immutable_set = frozenset([1, 2, 3, 4])
print(immutable_set)  # frozenset({1, 2, 3, 4})

# Cannot add or remove
# immutable_set.add(5)  # Error!

# Can be used as dictionary keys (unlike regular sets)
unique_coords = {frozenset([1, 2]), frozenset([3, 4])}

Characteristics:

  • Immutable version of sets.
  • Cannot be modified after creation.
  • Can be used as dictionary keys or set elements (regular sets cannot).

6. Collection Type Comparison Table

  • List

    • Ordered: Yes
    • Mutable: Yes
    • Duplicates: Yes
    • Indexed: Yes
    • Immutable: No
    • Use Case: General-purpose sequences
  • Tuple

    • Ordered: Yes
    • Mutable: No
    • Duplicates: Yes
    • Indexed: Yes
    • Immutable: Yes
    • Use Case: Return multiple values, hashable keys
  • Set

    • Ordered: No
    • Mutable: Yes
    • Duplicates: No
    • Indexed: No
    • Immutable: No
    • Use Case: Unique elements, fast membership test
  • Frozenset

    • Ordered: No
    • Mutable: No
    • Duplicates: No
    • Indexed: No
    • Immutable: Yes
    • Use Case: Immutable set, use as dict key
  • Dictionary

    • Ordered: Yes*
    • Mutable: Yes
    • Duplicates: N/A
    • Indexed: By key
    • Immutable: No
    • Use Case: Key-value mappings

*Insertion order preserved in Python 3.7+


7. Choosing the Right Collection

# Use LIST when you need ordered, mutable sequences
shopping_list = ["milk", "eggs", "bread"]

# Use TUPLE when you want immutable sequences (return values, function arguments)
def get_user():
    return ("Alice", 25, True)  # (name, age, is_active)

# Use SET when you need unique values or fast membership tests
unique_tags = {"python", "java", "rust"}

# Use DICTIONARY when you need fast lookups by key
user_data = {"id": 1, "name": "Alice", "email": "alice@example.com"}

# FROZENSET for immutable sets (less common)
immutable_coordinates = frozenset([(1, 2), (3, 4)])

8. Practical Examples

Example 1: Remove Duplicates from List

numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5]
unique = list(set(numbers))
print(sorted(unique))  # [1, 2, 3, 4, 5]

Example 2: Find Common Elements

list_a = [1, 2, 3, 4, 5]
list_b = [3, 4, 5, 6, 7]
common = set(list_a) & set(list_b)
print(common)  # {3, 4, 5}

Example 3: Group Data

students = [
    {"name": "Alice", "grade": "A"},
    {"name": "Bob", "grade": "B"},
    {"name": "Charlie", "grade": "A"}
]

by_grade = {}
for student in students:
    grade = student["grade"]
    if grade not in by_grade:
        by_grade[grade] = []
    by_grade[grade].append(student["name"])

print(by_grade)  # {'A': ['Alice', 'Charlie'], 'B': ['Bob']}

Example 4: Default Dictionary (when you expect multiple similar structures)

from collections import defaultdict

by_grade = defaultdict(list)
for student in students:
    by_grade[student["grade"]].append(student["name"])

print(by_grade)  # defaultdict(<class 'list'>, {'A': [...], 'B': [...]})