0
0
Pythonprogramming~5 mins

Adding and removing list elements in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you add an element to the end of a list in Python?
Use the append() method. For example, my_list.append(5) adds 5 to the end of my_list.
Click to reveal answer
beginner
What method removes and returns the last element from a list?
The pop() method removes and returns the last element. For example, item = my_list.pop() removes the last item and stores it in item.
Click to reveal answer
intermediate
How can you add an element at a specific position in a list?
Use the insert(index, element) method. It adds the element at the given index, shifting others to the right. Example: my_list.insert(1, 'a') adds 'a' at position 1.
Click to reveal answer
beginner
How do you remove a specific element by value from a list?
Use the remove(value) method. It deletes the first occurrence of the value. Example: my_list.remove(3) removes the first 3 found.
Click to reveal answer
intermediate
What happens if you try to remove an element that is not in the list using remove()?
Python raises a ValueError because the element is not found in the list.
Click to reveal answer
Which method adds an element to the end of a list?
Aappend()
Binsert()
Cremove()
Dpop()
What does pop() do when called without arguments?
ARemoves a specific value
BRemoves and returns the last element
CAdds an element to the end
DRemoves the first element
How do you add an element at index 2 in a list?
Amy_list.append(2)
Bmy_list.remove(2)
Cmy_list.insert(2, element)
Dmy_list.pop(2)
Which method removes the first occurrence of a value from a list?
Ainsert()
Bappend()
Cpop()
Dremove()
What error occurs if you try to remove a value not in the list?
AValueError
BIndexError
CTypeError
DKeyError
Explain how to add elements to a Python list and the difference between append() and insert().
Think about adding items to a shopping list either at the end or in the middle.
You got /3 concepts.
    Describe the ways to remove elements from a list and what happens if you remove a value not present.
    Consider taking items off a shelf by position or by name.
    You got /3 concepts.