Home Tutorials Python Programs Data Lists & Collections
Data Lists & Collections

Data Lists & Collections


Topic: Data Lists & Collections

Description: This section covers list manipulation, processing structural records, array mapping, and running computational data evaluations.

Why: Lists and collections are the primary vehicles for managing groups of structured data in Python. Learning how to cleanly extract records, filter criteria, and map values from collections allows you to build reliable backend data-processing algorithms and manage structured dynamic payloads.


Program 3.1: Find Largest and Smallest Items in a List

Problem Statement: Write a function that scans an un-ordered list of values and isolates the highest and lowest numbers without using any built-in sorting helper functions.

Expected Input & Output:

  • Input: [23, 5, 89, 14, 2] → Output: (89, 2)

Core Syntax Hint: Initialize tracking references using the initial index (list[0]), then compare remaining list positions item-by-item using a for loop.

def find_min_max(numbers):
    if not numbers:
        return None
    
    min_val = numbers[0]
    max_val = numbers[0]
    
    for num in numbers:
        if num > max_val:
            max_val = num
        if num < min_val:
            min_val = num
            
    return max_val, min_val

# Test Case Execution / Output Example
print(find_min_max([23, 5, 89, 14, 2]))  # Output: (89, 2)

Program 3.2: Filter Even Numbers (List Comprehension)

Problem Statement: Take an existing dataset tracking random integers and isolate all even values into a brand new standalone sub-list.

Expected Input & Output:

  • Input: [1, 2, 3, 4, 5, 6] → Output: [2, 4, 6]

Core Syntax Hint: Write clean, professional Python code using List Comprehensions to execute expressions inline based on conditional logic filter loops.

def extract_evens(numbers):
    # Syntax pattern: [expression for item in iterable if condition]
    return [num for num in numbers if num % 2 == 0]

# Test Case Execution / Output Example
print(extract_evens([1, 2, 3, 4, 5, 6]))  # Output: [2, 4, 6]

Program 3.3: Remove Duplicates from a List

Problem Statement: Strip out redundant duplicate item records from an array while carefully preserving the exact sequencing order of elements.

Expected Input & Output:

  • Input: ["apple", "banana", "apple", "cherry", "banana"] → Output: ["apple", "banana", "cherry"]

Core Syntax Hint: Create an empty tracking storage array and append elements conditionally only if they are not in that targeted destination stack.

def unique_items(items):
    seen = []
    for item in items:
        if item not in seen:
            seen.append(item)
    return seen

# Test Case Execution / Output Example
fruits = ["apple", "banana", "apple", "cherry", "banana"]
print(unique_items(fruits))  # Output: ['apple', 'banana', 'cherry']

Program 3.4: Cumulative Sum Generator

Problem Statement: Write a script that transforms an input array into a cumulative progression array where each step reflects the running total of previous elements.

Expected Input & Output:

  • Input: [1, 2, 3, 4] → Output: [1, 3, 6, 10]

Core Syntax Hint: Maintain an external running accumulator value variable outside your iteration, modifying it dynamically within your loop logic block execution.

def running_total(numbers):
    cumulative = []
    current_sum = 0
    for num in numbers:
        current_sum += num
        cumulative.append(current_sum)
    return cumulative

# Test Case Execution / Output Example
print(running_total([1, 2, 3, 4]))  # Output: [1, 3, 6, 10]

Program 3.5: Word Length Mapper (Dictionary Comprehension)

Problem Statement: Given an input list containing individual string values, process the items to output a structural Key-Value dictionary object matching specific words with their explicit character lengths.

Expected Input & Output:

  • Input: ["Python", "Code"] → Output: {"Python": 6, "Code": 4}

Core Syntax Hint: Use **Dictionary Comprehensions** ({key: value for item in iterable}) along with the built-in len() string utility to achieve short, single-line mapping efficiency.

def map_word_lengths(words):
    return {word: len(word) for word in words}

# Test Case Execution / Output Example
test_words = ["Python", "Code"]
print(map_word_lengths(test_words))  # Output: {'Python': 6, 'Code': 4}

🏋️ Test Yourself With Exercises

Take our quiz on Data Lists & Collections to test your knowledge.

Browse Quizzes »