Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

List Exercises

This directory contains exercises to help you practice working with Python lists. Lists are Python's most versatile data structure, equivalent to ArrayLists in Java but with additional powerful features.

Setup

  1. Open your terminal/command prompt
  2. Navigate to this directory
  3. Run the exercises: python lists.py
  4. Or work interactively: python3 -i lists.py

Exercises

Exercise 1: Basic List Operations

Write a function create_list(size, default_value) that creates a list of given size with default values. Write a function get_list_length(items) that returns the length of a list. Write a function add_item(items, item) that adds an item to the end of a list.

Exercise 2: List Access and Modification

Write a function get_first_item(items) that returns the first item, or None if empty. Write a function get_last_item(items) that returns the last item, or None if empty. Write a function replace_item(items, index, new_item) that replaces item at index.

Exercise 3: List Searching

Write a function find_item(items, target) that returns the index of target, -1 if not found. Write a function contains_item(items, target) that returns True if target is in the list. Write a function count_occurrences(items, target) that counts how many times target appears.

Exercise 4: List Statistics

Write a function find_maximum(numbers) that finds the largest number in a list. Write a function find_minimum(numbers) that finds the smallest number in a list. Write a function calculate_sum(numbers) that calculates the sum of all numbers. Write a function calculate_average(numbers) that calculates the average as a float.

Exercise 5: List Filtering and Transformation

Write a function filter_positive(numbers) that returns only positive numbers. Write a function filter_even(numbers) that returns only even numbers. Write a function square_numbers(numbers) that returns a list of squared numbers. Write a function filter_by_length(strings, min_length) that filters strings by minimum length.

Exercise 6: List Sorting and Ordering

Write a function sort_ascending(numbers) that returns a sorted copy (ascending). Write a function sort_descending(numbers) that returns a sorted copy (descending). Write a function reverse_list(items) that returns a reversed copy. Write a function sort_by_length(strings) that sorts strings by length.

Exercise 7: List Combination

Write a function combine_lists(list1, list2) that combines two lists into one. Write a function merge_sorted_lists(list1, list2) that merges two sorted lists into one sorted list. Write a function remove_duplicates(items) that removes duplicates while preserving order. Write a function intersect_lists(list1, list2) that returns items common to both lists.

Exercise 8: List Slicing and Chunking

Write a function get_first_n(items, n) that returns the first n items. Write a function get_last_n(items, n) that returns the last n items. Write a function get_middle_items(items) that returns items excluding first and last. Write a function chunk_list(items, chunk_size) that splits list into chunks of given size.

Exercise 9: Advanced List Operations

Write a function flatten_nested_list(nested_list) that flattens a list of lists. Write a function rotate_list(items, positions) that rotates list left by positions. Write a function zip_lists(list1, list2) that combines corresponding elements into tuples. Write a function group_by_first_letter(words) that groups words by their first letter.

Testing Your Code

# Example tests
print(create_list(3, 0))                    # Should return [0, 0, 0]
print(find_item([1, 2, 3], 2))              # Should return 1
print(filter_positive([-1, 2, -3, 4]))      # Should return [2, 4]
print(sort_ascending([3, 1, 4, 1, 5]))      # Should return [1, 1, 3, 4, 5]
print(combine_lists([1, 2], [3, 4]))        # Should return [1, 2, 3, 4]
print(get_first_n([1, 2, 3, 4, 5], 3))      # Should return [1, 2, 3]
print(flatten_nested_list([[1, 2], [3, 4]])) # Should return [1, 2, 3, 4]

Python List Features

  • Dynamic size: Lists grow and shrink automatically
  • Mixed types: Can contain different data types
  • Negative indexing: list[-1] gets the last item
  • Slicing: list[start:end:step] for powerful subsetting
  • List comprehensions: [x*2 for x in numbers if x > 0]
  • Multiple assignment: a, b, c = [1, 2, 3]
  • Unpacking: func(*list) passes list items as arguments

Tips

  • Use list comprehensions for transformations: [x*2 for x in numbers]
  • append() adds one item, extend() adds multiple items
  • insert(index, item) inserts at specific position
  • remove(item) removes first occurrence, pop() removes by index
  • sort() modifies the list, sorted() returns a new sorted list
  • in operator for membership testing: item in list
  • Use enumerate() when you need both index and value