All Flashcards
How are lists applied in real-world scenarios?
Storing a sequence of tasks, managing a playlist of songs, representing a deck of cards.
How is list traversal used in data analysis?
Iterating through data points to calculate statistics or identify patterns.
How are lists used in implementing stacks and queues?
Lists can be used as the underlying data structure for stacks (LIFO) and queues (FIFO).
How are lists used in game development?
Storing the positions of game objects, managing player inventories, and tracking game states.
How are lists used in web development?
Storing lists of users, items in a shopping cart, or search results.
How can lists be used to implement a simple database?
Each list can represent a table, with each element in the list representing a row or record.
How are lists used in machine learning?
Storing feature vectors, training data, and model parameters.
How are lists used in social media applications?
Storing lists of friends, posts, and comments.
How are lists used in recommendation systems?
Storing lists of recommended items based on user preferences.
How are lists used in operating systems?
Managing processes, storing file directories, and handling system calls.
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.
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.