This List Methods lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
append adds one item at the end. insert places an item at an index. pop removes by index and returns it. remove deletes the first matching value.
index and count search the list. clear empties it.
Example 1: Start here
pop with no argument removes the last item.
skills = ["Python", "SQL"] # a list; the values stay in this order
skills.insert(1, "Git") # put the item at that position
skills.remove("SQL") # remove the first matching item
print(skills)
print(skills.pop())
Example 2: Insert and pop
insert places an item at an index. pop removes the last item and returns it.
skills = ["Python", "Git"] # a list; the values stay in this order
skills.insert(1, "SQL") # put the item at that position
print(skills)
print(skills.pop())
Example 3: Remove a value
remove deletes the first matching value.
skills = ["Python", "SQL", "Git"] # a list; the values stay in this order
skills.remove("SQL") # remove the first matching item
print(skills)
Example 4: Count and index
count tells you how many matches. index tells you where the first match sits.
scores = [80, 90, 80] # a list; the values stay in this order
print(scores.count(80)) # how many times it appears
print(scores.index(90)) # where the first match sits
