All Flashcards
What are the steps to insert an element into a list at a specific index?
- Call the
insert()method on the list. 2. Provide the index where you want to insert the element. 3. Provide the value of the element to be inserted.
What are the steps to remove the first occurrence of a specific value from a list?
- Call the
remove()method on the list. 2. Provide the value you want to remove. 3. The method searches for the first instance of the value and removes it.
What are the steps to traverse a list using a for loop?
- Initialize the list. 2. Start a
forloop that iterates through each element in the list. 3. Access and process each element within the loop.
What are the steps to traverse a list using a while loop?
- Initialize the list. 2. Initialize a counter variable (e.g.,
i = 0). 3. Start awhileloop that continues as long as the counter is less than the length of the list. 4. Access the element at the current index (list[i]). 5. Increment the counter (i += 1).
What are the steps to perform a linear search in a list?
- Start at the first element of the list. 2. Compare the current element with the target value. 3. If they match, return the index of the current element. 4. If they don't match, move to the next element and repeat steps 2-4. 5. If the target value is not found after checking all elements, return -1 (or a similar indicator).
What are the steps to add an element to the end of a list?
- Call the
append()method on the list. 2. Provide the value of the element to be added.
What are the steps to assign a value to an element at a specific index?
- Access the element using its index:
list_name[index]. 2. Use the assignment operator (=) to assign the new value:list_name[index] = new_value.
What are the steps to find the length of a list?
- Call the
len()function. 2. Pass the list as an argument to the function:len(list_name). 3. The function returns the number of elements in the list.
What are the steps to initialize a list?
- Choose a name for the list. 2. Use square brackets
[]to define the list. 3. Add elements inside the square brackets, separated by commas.
What are the steps to access an element of a list?
- Write the name of the list. 2. Write the index of the element you want to access inside square brackets:
list_name[index].
How do you access an element in a list?
Using its index: list_name[index].
How do you change the value of an element in a list?
Assign a new value to the element at its index: list_name[index] = new_value.
How do you insert an element into a list?
Use the insert() method: list_name.insert(index, value).
How do you add an element to the end of a list?
Use the append() method: list_name.append(value).
How do you remove an element from a list?
Use the remove() method: list_name.remove(value).
How do you determine the number of elements in a list?
Use the len() function: len(list_name).
Explain complete list traversal.
Iterating through every element of the list, typically using a for or while loop.
Explain partial list traversal.
Iterating through a specific portion of the list, defined by start and end indices.
Describe the efficiency of linear search.
Simple to implement but inefficient for large lists, as it checks each element sequentially.
How does indexing work in Python lists?
Indexing starts at 0 for the first element.
What does the following code output?
python
my_list = [10, 20, 30]
print(my_list[1])
20
What does the following code output?
python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
[1, 2, 3, 4]
What does the following code output?
python
my_list = [5, 10, 15]
my_list.insert(1, 7)
print(my_list)
[5, 7, 10, 15]
What does the following code output?
python
my_list = ['a', 'b', 'c']
my_list.remove('b')
print(my_list)
['a', 'c']
What does the following code output?
python
my_list = [10, 20, 30, 20]
my_list.remove(20)
print(my_list)
[10, 30, 20]
What does the following code output?
python
my_list = [1, 2, 3]
print(len(my_list))
3
What does the following code output?
python
my_list = [1, 2, 3, 4, 5]
for i in range(2):
print(my_list[i])
1 2
What does the following code output?
python
my_list = [10, 20, 30, 40]
i = 1
while i < 3:
print(my_list[i])
i += 1
20 30
What does the following code output?
python
my_list = [1, 2, 3]
my_list[0] = my_list[2]
print(my_list)
[3, 2, 3]
Identify the error in the following code:
python
my_list = [1, 2, 3]
print(my_list[3])
IndexError: list index out of range. The valid indices are 0, 1, and 2.