Lists - AP Computer Science Principles

  1. What is the result of list.count(x)?
    The number of times xx appears in the list. Returns frequency of occurrence for the specified element.
  2. How do you reverse a list in place?
    list.reverse(). Reverses elements in-place without creating a new list.
  3. Identify the syntax: [x for x in list if x > 0]
    List comprehension filtering positive elements. Combines iteration with conditional filtering in one expression.
  4. How do you create an empty list in Python?
    list = []. Creates an empty list using square bracket notation.
  5. Which method removes an element by value in a list?
    list.remove(value). Removes the first matching value from the list.
  6. Identify the operation: list.append(x)
    Adds element xx to the end of the list. Appends to the end, increasing list length by 1.
  7. What does list.sort() do?
    Sorts the list in ascending order. Modifies the original list in-place using default ascending order.
  8. Identify the operation: list.insert(index, x)
    Inserts element xx at the specified index. Places element at specific position, shifting others right.
  9. How do you iterate over a list with a for loop?
    for element in list:. Iterates through each element without using indices.
  10. What does list.index(x) return?
    The index of the first occurrence of xx in the list. Returns position of first match; raises ValueError if not found.
  11. What is a nested list?
    A list that contains other lists as elements. Enables creation of multi-dimensional data structures.
  12. What does 'in' keyword check in lists?
    Checks if an element exists in the list. Returns True if element found, False otherwise.
  13. What does list.copy() return?
    A shallow copy of the list. Creates a new list object with the same elements.
  14. What is the complexity of list.append(x)?
    O(1). Constant time since it adds to the end only.
  15. Identify the method to concatenate two lists.
    list1 + list2. Plus operator combines lists into a new list object.
  16. How do you access the last element of a list?
    list[-1]. Negative indexing starts from the end of the list.
  17. What does list.extend(iterable) do?
    Adds elements from the iterable to the end of the list. Extends the list with multiple elements from another collection.
  18. What is the purpose of a list index?
    To access a specific element in the list by its position. Indices provide direct access to elements without iteration.
  19. What is slicing in the context of lists?
    Extracting a portion of the list using indices. Creates sublists using start and end index boundaries.
  20. What type of data structure is a list in Python?
    Mutable, ordered collection. Lists can be modified after creation and maintain element order.
  21. What is the default step in list slicing?
    1. When no step specified, moves one position at a time.
  22. What does list[start:end] return?
    A sublist from index start to end-1. End index is exclusive, so it's not included in result.
  23. Which function returns the sum of elements in a list?
    sum(list). Built-in function that adds all numeric elements together.
  24. How do you join a list of strings into a single string?
    'separator'.join(list). Combines string elements with specified separator between them.
  25. Which method returns the largest item in a list?
    max(list). Built-in function that finds the largest comparable element.
  26. How do you check list equality in Python?
    Using the '==' operator. Compares element by element for both content and order.
  27. Identify error: list.pop(-1) on empty list.
    IndexError: pop from empty list. Cannot remove elements from a list with no elements.
  28. What is the complexity of list.pop()?
    O(1) for last element, O(n) for others. Last element removal is instant; others require shifting.
  29. Which method returns the smallest item in a list?
    min(list). Built-in function that finds the smallest comparable element.
  30. What is the result of len(list)?
    The number of elements in the list. Built-in function that counts all elements in the list.
  31. Which method removes an element by index in a list?
    list.pop(index). Removes and returns the element at the specified position.
  32. What is the index of the first element in a list?
    0. Lists use zero-based indexing in most programming languages.
  33. What does list comprehension [x**2 for x in list] produce?
    A list of squares of elements. Applies exponentiation operation to each list element.
  34. How do you convert a string to a list of characters?
    list(string). Converts each character into a separate list element.
  35. Find the error: list[1] = 100 on list of length 1.
    IndexError: list index out of range. Index 1 doesn't exist in a list with only one element.
  36. What is the result of list[::-1]?
    The list in reverse order. Negative step creates a reversed copy of the list.
  37. What is a list comprehension?
    A concise way to create lists. Enables functional programming style for list creation.
  38. What is a list in computer science?
    A collection of elements, typically of the same type, in a specific order. An ordered data structure that allows indexing and modification.
  39. What operation does list.clear() perform?
    Removes all elements from the list. Empties the list completely, leaving it with zero elements.

Study these flashcards