08 - LISTS
Python Programming Study Guide: Lists as Mutable Sequence Types
Bert’s contempt for Lists knows no bounds!
List Syntax
Bert made a summary of the syntax for each of the Python list functions in the study guide:
append()
- Adds an element to the end of the list.
- Syntax:
list.append(element)
index()
- Returns the index of the first occurrence of the specified element.
- Syntax:
list.index(element, start, end)
- The
start
andend
parameters are optional and specify the search range.
insert()
- Inserts an element at the specified position.
- Syntax:
list.insert(index, element)
pop()
- Removes and returns the element at the specified position.
- Syntax:
list.pop(index)
- If the index is not specified,
pop()
removes and returns the last element.
remove()
- Removes the first occurrence of the specified element.
- Syntax:
list.remove(element)
reverse()
- Reverses the elements of the list in place.
- Syntax:
list.reverse()
count()
- Returns the number of times the specified element appears in the list.
- Syntax:
list.count(element)
Key Concepts
Lists as Mutable Sequence Types
- Mutable: Lists in Python can be changed after they are created. This is in contrast to strings, which are immutable.
- Sequence Type: A list is an ordered collection of items.
Indexing and Slicing
- Indexing: Access an individual element of a list using its index, e.g.,
list[0]
for the first item. - Slice Notation: Extract a subset of a list, e.g.,
list[start:stop]
extracts elements from indexstart
tostop-1
.
List Functions and Operations
- Add:
append(item)
adds an item to the end,insert(index, item)
adds an item at a specified index. - Remove:
remove(item)
removes the first occurrence of an item. - Update: Directly assign a new value to a list element, e.g.,
list[index] = new_value
. - Find: Use
index(item)
to find the index of the first occurrence of an item.
Common Patterns for List Management
- Enumeration and Aggregates: Using loops to iterate over lists, and using functions like
len()
,max()
,min()
to aggregate data. - List Mutation: Changing, adding, or removing elements in a list.
- Combining Lists: Use the
+
operator to concatenate two lists.
Multiple Choice Questions
- For
x = [0,1,2,3,4,5]
, what isx[2:4]
?- A. [2,3]
- B. [1,2,3]
- C. [1,2]
- D. [2,3,4,5]
- How do you add a new element at the end of a list named
myList
?- A.
myList.append(newElement)
- B.
myList.insert(len(myList), newElement)
- C.
myList.add(newElement)
- D.
myList.put(newElement)
- A.
- Which operation will combine two lists
list1
andlist2
into a new listlist3
?- A.
list3 = list1 + list2
- B.
list3 = list1.append(list2)
- C.
list3 = list1.extend(list2)
- D.
list3 = combine(list1, list2)
- A.
- What does
grades[:2]
return ifgrades = ['A', 'B+','A','C+','B-']
?- A. [‘A’, ‘B+’]
- B. [‘B+’, ‘A’]
- C. [‘A’, ‘B+’, ‘A’]
- D. The first two characters of the first item in
grades
- What is the result of the following operations on the list
items = ['a','b','c','d']
?
- A. [‘a’,‘b’,‘c’,‘d’, ‘w’]
- B. [‘a’,‘b’,‘d’, ‘w’]
- C. [‘x’, ‘b’, ‘d’, ‘w’]
- D. [‘i’,‘dont’,‘know’]
What is the output of the following code?
= [4, 2, 8, 6] myList 2] = 5 myList[print(myList)
- A.
[4, 2, 5, 6]
- B.
[4, 2, 8, 5]
- C.
[4, 5, 8, 6]
- D.
[5, 2, 8, 6]
- A.
What is the output of the following code?
= ["apple", "banana", "cherry"] x "banana") x.remove(print(x)
- A.
["apple", "banana"]
- B.
["apple", "cherry"]
- C.
["banana", "cherry"]
- D.
["apple", "banana", "cherry"]
- A.
What will be the length of the list after executing the following code?
= [1, 2, 3, 4, 5] x 6) x.append(7, 8]) x.extend([
- A.
5
- B.
6
- C.
8
- D.
10
- A.
What is the result of the following list slicing operation?
= [10, 20, 30, 40, 50] myList print(myList[1:4])
- A.
[10, 20, 30]
- B.
[20, 30, 40]
- C.
[30, 40, 50]
- D.
[20, 30, 40, 50]
- A.
Which method is used to add an element at a specific index in a list?
- A.
append()
- B.
insert()
- C.
extend()
- D.
add()
- A.
Correct Answers and Explanations
- Answer: A. [2,3]
- Explanation: The slice
x[2:4]
extracts elements from index 2 up to but not including index 4, resulting in[2, 3]
.
- Answer: A.
myList.append(newElement)
- Explanation:
append()
is the correct method to add an element to the end of a list. The other options are either incorrect methods or overly complicated.
- Answer: A.
list3 = list1 + list2
- Explanation: The
+
operator concatenates two lists.append()
andextend()
methods modify the list in place and do not return a new list.
- Answer: A. [‘A’, ‘B+’]
- Explanation: The slice
grades[:2]
returns the first two elements of the list, which are [‘A’, ‘B+’].
- Answer: C. [‘x’, ‘b’, ‘d’, ‘w’]
- Explanation: After each operation, the list changes as follows:
items.append('w')
-> [‘a’,‘b’,‘c’,‘d’, ‘w’]items.remove('c')
-> [‘a’,‘b’,‘d’, ‘w’]items.insert(0,'x')
-> [‘x’, ‘a’, ‘b’, ‘d’, ‘w’]items.pop(1)
-> [‘x’, ‘b’, ‘d’, ‘w’]
Answer: A.
[4, 2, 5, 6]
Explanation: The code changes the element at index 2 (third element) from 8 to 5.
Answer: B.
["apple", "cherry"]
Explanation: The
remove()
method removes the first occurrence of the element “banana” from the list.Answer: C.
8
Explanation: The
append()
method adds one element, andextend()
adds three more elements, making the total length 8.Answer: B.
[20, 30, 40]
Explanation: The slice
[1:4]
includes elements from index 1 to 3 (20, 30, 40).Answer: B.
insert()
Explanation: The
insert()
method is used to add an element at a specified index.append()
adds to the end,extend()
adds multiple elements, andadd()
is not a list method. ```
This format should correctly render in Quarto, displaying both the questions and their answers in a structured and readable format.