Lists - AP Computer Science Principles
Card 1 of 30
What operation does list.clear() perform?
What operation does list.clear() perform?
Tap to reveal answer
Removes all elements from the list. Empties the list completely, leaving it with zero elements.
Removes all elements from the list. Empties the list completely, leaving it with zero elements.
← Didn't Know|Knew It →
What does list.copy() return?
What does list.copy() return?
Tap to reveal answer
A shallow copy of the list. Creates a new list object with the same elements.
A shallow copy of the list. Creates a new list object with the same elements.
← Didn't Know|Knew It →
What is the index of the first element in a list?
What is the index of the first element in a list?
Tap to reveal answer
- Lists use zero-based indexing in most programming languages.
- Lists use zero-based indexing in most programming languages.
← Didn't Know|Knew It →
What is the purpose of a list index?
What is the purpose of a list index?
Tap to reveal answer
To access a specific element in the list by its position. Indices provide direct access to elements without iteration.
To access a specific element in the list by its position. Indices provide direct access to elements without iteration.
← Didn't Know|Knew It →
Identify the operation: list.append(x)
Identify the operation: list.append(x)
Tap to reveal answer
Adds element $x$ to the end of the list. Appends to the end, increasing list length by 1.
Adds element $x$ to the end of the list. Appends to the end, increasing list length by 1.
← Didn't Know|Knew It →
Which method removes an element by value in a list?
Which method removes an element by value in a list?
Tap to reveal answer
list.remove(value). Removes the first matching value from the list.
list.remove(value). Removes the first matching value from the list.
← Didn't Know|Knew It →
What does list.index(x) return?
What does list.index(x) return?
Tap to reveal answer
The index of the first occurrence of $x$ in the list. Returns position of first match; raises ValueError if not found.
The index of the first occurrence of $x$ in the list. Returns position of first match; raises ValueError if not found.
← Didn't Know|Knew It →
What does list.sort() do?
What does list.sort() do?
Tap to reveal answer
Sorts the list in ascending order. Modifies the original list in-place using default ascending order.
Sorts the list in ascending order. Modifies the original list in-place using default ascending order.
← Didn't Know|Knew It →
What is the result of list.count(x)?
What is the result of list.count(x)?
Tap to reveal answer
The number of times $x$ appears in the list. Returns frequency of occurrence for the specified element.
The number of times $x$ appears in the list. Returns frequency of occurrence for the specified element.
← Didn't Know|Knew It →
How do you access the last element of a list?
How do you access the last element of a list?
Tap to reveal answer
list[-1]. Negative indexing starts from the end of the list.
list[-1]. Negative indexing starts from the end of the list.
← Didn't Know|Knew It →
What is a list in computer science?
What is a list in computer science?
Tap to reveal answer
A collection of elements, typically of the same type, in a specific order. An ordered data structure that allows indexing and modification.
A collection of elements, typically of the same type, in a specific order. An ordered data structure that allows indexing and modification.
← Didn't Know|Knew It →
What does list.extend(iterable) do?
What does list.extend(iterable) do?
Tap to reveal answer
Adds elements from the iterable to the end of the list. Extends the list with multiple elements from another collection.
Adds elements from the iterable to the end of the list. Extends the list with multiple elements from another collection.
← Didn't Know|Knew It →
Find the error: list[1] = 100 on list of length 1.
Find the error: list[1] = 100 on list of length 1.
Tap to reveal answer
IndexError: list index out of range. Index 1 doesn't exist in a list with only one element.
IndexError: list index out of range. Index 1 doesn't exist in a list with only one element.
← Didn't Know|Knew It →
How do you reverse a list in place?
How do you reverse a list in place?
Tap to reveal answer
list.reverse(). Reverses elements in-place without creating a new list.
list.reverse(). Reverses elements in-place without creating a new list.
← Didn't Know|Knew It →
Which method returns the smallest item in a list?
Which method returns the smallest item in a list?
Tap to reveal answer
min(list). Built-in function that finds the smallest comparable element.
min(list). Built-in function that finds the smallest comparable element.
← Didn't Know|Knew It →
What type of data structure is a list in Python?
What type of data structure is a list in Python?
Tap to reveal answer
Mutable, ordered collection. Lists can be modified after creation and maintain element order.
Mutable, ordered collection. Lists can be modified after creation and maintain element order.
← Didn't Know|Knew It →
Identify the operation: list.insert(index, x)
Identify the operation: list.insert(index, x)
Tap to reveal answer
Inserts element $x$ at the specified index. Places element at specific position, shifting others right.
Inserts element $x$ at the specified index. Places element at specific position, shifting others right.
← Didn't Know|Knew It →
How do you iterate over a list with a for loop?
How do you iterate over a list with a for loop?
Tap to reveal answer
for element in list:. Iterates through each element without using indices.
for element in list:. Iterates through each element without using indices.
← Didn't Know|Knew It →
What does 'in' keyword check in lists?
What does 'in' keyword check in lists?
Tap to reveal answer
Checks if an element exists in the list. Returns True if element found, False otherwise.
Checks if an element exists in the list. Returns True if element found, False otherwise.
← Didn't Know|Knew It →
Identify the method to concatenate two lists.
Identify the method to concatenate two lists.
Tap to reveal answer
list1 + list2. Plus operator combines lists into a new list object.
list1 + list2. Plus operator combines lists into a new list object.
← Didn't Know|Knew It →
What is the result of len(list)?
What is the result of len(list)?
Tap to reveal answer
The number of elements in the list. Built-in function that counts all elements in the list.
The number of elements in the list. Built-in function that counts all elements in the list.
← Didn't Know|Knew It →
What does list[start:end] return?
What does list[start:end] return?
Tap to reveal answer
A sublist from index start to end-1. End index is exclusive, so it's not included in result.
A sublist from index start to end-1. End index is exclusive, so it's not included in result.
← Didn't Know|Knew It →
What is a nested list?
What is a nested list?
Tap to reveal answer
A list that contains other lists as elements. Enables creation of multi-dimensional data structures.
A list that contains other lists as elements. Enables creation of multi-dimensional data structures.
← Didn't Know|Knew It →
Identify error: list.pop(-1) on empty list.
Identify error: list.pop(-1) on empty list.
Tap to reveal answer
IndexError: pop from empty list. Cannot remove elements from a list with no elements.
IndexError: pop from empty list. Cannot remove elements from a list with no elements.
← Didn't Know|Knew It →
Which function returns the sum of elements in a list?
Which function returns the sum of elements in a list?
Tap to reveal answer
sum(list). Built-in function that adds all numeric elements together.
sum(list). Built-in function that adds all numeric elements together.
← Didn't Know|Knew It →
What is the result of list[::-1]?
What is the result of list[::-1]?
Tap to reveal answer
The list in reverse order. Negative step creates a reversed copy of the list.
The list in reverse order. Negative step creates a reversed copy of the list.
← Didn't Know|Knew It →
What is the default step in list slicing?
What is the default step in list slicing?
Tap to reveal answer
- When no step specified, moves one position at a time.
- When no step specified, moves one position at a time.
← Didn't Know|Knew It →
Identify the syntax: [x for x in list if x > 0]
Identify the syntax: [x for x in list if x > 0]
Tap to reveal answer
List comprehension filtering positive elements. Combines iteration with conditional filtering in one expression.
List comprehension filtering positive elements. Combines iteration with conditional filtering in one expression.
← Didn't Know|Knew It →
What is a list comprehension?
What is a list comprehension?
Tap to reveal answer
A concise way to create lists. Enables functional programming style for list creation.
A concise way to create lists. Enables functional programming style for list creation.
← Didn't Know|Knew It →
How do you check list equality in Python?
How do you check list equality in Python?
Tap to reveal answer
Using the '==' operator. Compares element by element for both content and order.
Using the '==' operator. Compares element by element for both content and order.
← Didn't Know|Knew It →