Lists - AP Computer Science Principles
- What is the result of list.count(x)?
- The number of times appears in the list. Returns frequency of occurrence for the specified element.
- How do you reverse a list in place?
- list.reverse(). Reverses elements in-place without creating a new list.
- Identify the syntax: [x for x in list if x > 0]
- List comprehension filtering positive elements. Combines iteration with conditional filtering in one expression.
- How do you create an empty list in Python?
- list = []. Creates an empty list using square bracket notation.
- Which method removes an element by value in a list?
- list.remove(value). Removes the first matching value from the list.
- Identify the operation: list.append(x)
- Adds element to the end of the list. Appends to the end, increasing list length by 1.
- What does list.sort() do?
- Sorts the list in ascending order. Modifies the original list in-place using default ascending order.
- Identify the operation: list.insert(index, x)
- Inserts element at the specified index. Places element at specific position, shifting others right.
- How do you iterate over a list with a for loop?
- for element in list:. Iterates through each element without using indices.
- What does list.index(x) return?
- The index of the first occurrence of in the list. Returns position of first match; raises ValueError if not found.
- What is a nested list?
- A list that contains other lists as elements. Enables creation of multi-dimensional data structures.
- What does 'in' keyword check in lists?
- Checks if an element exists in the list. Returns True if element found, False otherwise.
- What does list.copy() return?
- A shallow copy of the list. Creates a new list object with the same elements.
- What is the complexity of list.append(x)?
- O(1). Constant time since it adds to the end only.
- Identify the method to concatenate two lists.
- list1 + list2. Plus operator combines lists into a new list object.
- How do you access the last element of a list?
- list[-1]. Negative indexing starts from the end of the list.
- 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.
- 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.
- What is slicing in the context of lists?
- Extracting a portion of the list using indices. Creates sublists using start and end index boundaries.
- What type of data structure is a list in Python?
- Mutable, ordered collection. Lists can be modified after creation and maintain element order.
- What is the default step in list slicing?
- 1. When no step specified, moves one position at a time.
- 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.
- Which function returns the sum of elements in a list?
- sum(list). Built-in function that adds all numeric elements together.
- How do you join a list of strings into a single string?
- 'separator'.join(list). Combines string elements with specified separator between them.
- Which method returns the largest item in a list?
- max(list). Built-in function that finds the largest comparable element.
- How do you check list equality in Python?
- Using the '==' operator. Compares element by element for both content and order.
- Identify error: list.pop(-1) on empty list.
- IndexError: pop from empty list. Cannot remove elements from a list with no elements.
- What is the complexity of list.pop()?
- O(1) for last element, O(n) for others. Last element removal is instant; others require shifting.
- Which method returns the smallest item in a list?
- min(list). Built-in function that finds the smallest comparable element.
- What is the result of len(list)?
- The number of elements in the list. Built-in function that counts all elements in the list.
- Which method removes an element by index in a list?
- list.pop(index). Removes and returns the element at the specified position.
- What is the index of the first element in a list?
- 0. Lists use zero-based indexing in most programming languages.
- What does list comprehension [x**2 for x in list] produce?
- A list of squares of elements. Applies exponentiation operation to each list element.
- How do you convert a string to a list of characters?
- list(string). Converts each character into a separate list element.
- 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.
- What is the result of list[::-1]?
- The list in reverse order. Negative step creates a reversed copy of the list.
- What is a list comprehension?
- A concise way to create lists. Enables functional programming style for list creation.
- 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.
- What operation does list.clear() perform?
- Removes all elements from the list. Empties the list completely, leaving it with zero elements.